N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judg
ment, they visited the neighborhood ‘watering hole‘ and drank a few too many beers before dinner. Wh
en it was time to line up for their evening meal, they did not line up in the required ascending num
erical 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 s
illy 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 c
ows.
1~n,乱序排列,告诉每个位置的前面的数字中比它小的数的个数,求每个位置的数字是多少
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=8000+10;
int tree[maxn<<2];
int a[maxn],n;
int ans[maxn];
void pushup(int x){
tree[x]=tree[x<<1]+tree[x<<1|1];
}
void change(int l,int r,int rt,int R,int c){
if(l==r) {
tree[rt]+=c;
return ;
}
int mid=(l+r)>>1;
if(R<=mid) change(l,mid,rt<<1,R,c);
else change(mid+1,r,rt<<1|1,R,c);
pushup(rt);
}
int ask(int l,int r,int rt,int L,int R){
if(l>=L&&r<=R) return tree[rt];
int mid=(l+r)>>1;
int ans=0;
if(L<=mid) ans+=ask(l,mid,rt<<1,L,R);
if(R>mid) ans+=ask(mid+1,r,rt<<1|1,L,R);
return ans;
}
int solve(int x){
int l=1,r=n;
while(l<=r)
{
int mid=(l+r)/2;
if(ask(1,n,1,1,mid)<x)
l=mid+1;
else r=mid-1;
}
return l;
}
void build(int l,int r,int rt){
if(l==r){
tree[rt]=1;
return ;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}
int main(){
scanf("%d",&n);
for (int i=2;i<=n;i++){
scanf("%d",&a[i]);
}
int top=0;
build(1,n,1);
for (int i=n;i>=1;i--){
ans[i]=solve(a[i]+1);
change(1,n,1,ans[i],-1);
}
for (int i=1;i<=n;i++) printf("%d\n",ans[i]);
return 0;
}