Lost Cows(线段树+二分判定)

4835: [Usaco2003 Open]Lost Cows

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 21  Solved: 17
[Submit][Status][Web Board]

Description

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,乱序排列,告诉每个位置的前面的数字中比它小的数的个数,求每个位置的数字是多少 

Input

* 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 i
n slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in sl
ot #3; and so on.

Output

* 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. 

Sample Input

5 
1 
2 
1 
0 

Sample Output

2 
4 
5 
3 
1 

#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;
}

 

posted @ 2018-06-18 13:27  lmjer  阅读(350)  评论(0编辑  收藏  举报