题目描述:
N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'watering hole' and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands.
Regrettably, FJ does not have a way to sort them. Furthermore, he's not very good at observing problems. Instead of writing down each cow's brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.
Given this data, tell FJ the exact ordering of the cows.
大意:
N(2<=N<=8000)头奶牛拥有1..N范围内的独特品牌。他们参观了附近的“水坑”,并在晚餐前喝了太多啤酒。当该排队吃晚餐时,他们并没有按照品牌的升序排列。
遗憾的是,FJ没有办法对它们进行排序。此外,他不太善于观察问题。他没有写下每头牛的品牌,而是确定了一个相当愚蠢的统计数字:对于每一头奶牛,他知道排在这头奶牛前面的奶牛的数量,事实上,这些奶牛的品牌比这头奶牛小。
根据这些数据,告诉FJ奶牛的确切顺序。
* Line 1: A single integer, N
* Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.
* Lines 1..N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.
*第1..N行:每N行输出告诉一头牛的品牌。输出的第1行告诉第一头牛的品牌;第二行告诉第二头牛的品牌;等等
输入 #1
5
1
2
1
0
输出 #1
2
4
5
3
1
思路:
倒序查询,最后一个的排名一定是ai+1,排除其影响,继续从后往前依次查询,重复遍历全部即可。
代码:
#include <iostream>
#include <string.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[10000]={},sum1[10000]={};
bool b[10000]={};
memset(b,false,sizeof(b));
for(int i=2;i<=n;i++){
cin>>a[i];
}
for(int i=n;i>0;i--){
int sum=0;
for(int j=1;j<=n;j++){
if(sum==a[i]&&b[j]==false){
b[j]=true;
sum1[i]=j;
break;
}
if(b[j]==false){
sum++;
}
}
}
for(int i=1;i<=n;i++){
printf("%d\n",sum1[i]);
}
return 0;
}