Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8341 | Accepted: 5320 |
Description
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.
Input
* 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.
Output
Sample Input
5 1 2 1 0
Sample Output
2 4 5 3 1
Source
#include<iostream> #include<cstring> #include<cstdio> using namespace std; const int MAXN=8000+100; int n; int C[MAXN],Lowbit[MAXN],data[MAXN],ans[MAXN]; //***************************************************************** //C[i] = a[i-lowbit(i)+1] + …+ a[i],下表从1开始 //Lowbit[i]=i&(i^(i-1));或Lowbit[i]=i&(-i); //1.查询 int QuerySum(int p) //查询原数组中下标1-p的元素的和 { int nSum=0; while(p>0) { nSum+=C[p]; p-=Lowbit[p]; } return nSum; } //2.修改+初始化 void Modify(int p,int val) //原数组中下表为p的元素+val,导致C[]数组中部分元素值的改变 { while(p<=MAXN-10) { C[p]+=val; p+=Lowbit[p]; } } //**************************************************************** //二分枚举返回当前满足条件的最小下表 int Binary_Find(int a) { int low=1,high=n,mid; while(low<=high) { mid=(low+high)>>1; if(mid-QuerySum(mid)<a) low=mid+1; else high=mid-1; } return low; } int main() { int i; for(i=1;i<MAXN;i++) Lowbit[i]=i&(-i); while(~scanf("%d",&n)) { memset(C,0,sizeof(C)); data[0]=0; for(i=1;i<n;i++) scanf("%d",&data[i]); for(i=n-1;i>=0;i--) { ans[i]=Binary_Find(data[i]+1); Modify(ans[i],1); } for(i=0;i<n;i++) printf("%d\n",ans[i]); } return 0; }