POJ 2182 Lost Cows

Lost Cows

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 1
Problem Description
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.  
 

 

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

 

Source
PKU
 
 
 
 

题目大意:一群牛的编号为1~n,现在乱站成一列,fj记录下来了每头牛前面编号比它编号小的牛的头数,求各个位置上牛的编号

解题思路:从后往前推,假如排在最后的一头牛比他编号小的数量为a,那么它的编号必然为a+1。我们把编号为a+1的这头牛删掉,假如排在倒数第二的一头牛比他编号小的数量为b,那么该牛就为删掉编号后剩余牛中的第b+1头牛,我们可以照此思路下去...

问题就可以转化为搜索排在第k位的数为多少,我们可以用线段树来实现。

 

 

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int N=8010;

#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)

struct Tree{
    int l,r;
    int num;     // num 记录还有多少头牛没去掉
}tree[N<<2];

void build(int L,int R,int rt){
    tree[rt].l=L;
    tree[rt].r=R;
    tree[rt].num=R-L+1;
    if(tree[rt].l==tree[rt].r)
        return ;
    int mid=(L+R)>>1;
    build(L,mid,L(rt));
    build(mid+1,R,R(rt));
}

int query(int id,int rt){   //搜索排在第id位的牛的编号
    tree[rt].num--;         //每搜索一次,牛的头数就减少一头
    if(tree[rt].l==tree[rt].r)
        return tree[rt].l;
    if(id<=tree[L(rt)].num) //说明左子树中还有足够的位置使该牛排在第id位
        query(id,L(rt));
    else
        query(id-tree[L(rt)].num,R(rt));    //左子树没有足够的位置,那么它就排在右子树的id-cow[rt<<1].num位
    //return 0;
}

int main(){

    //freopen("input.txt","r",stdin);

    int a[N],res[N],n;
    while(~scanf("%d",&n)){
        a[0]=0;             //第一个位置前没有牛,置为0
        for(int i=1;i<n;i++)
            scanf("%d",&a[i]);
        build(1,n,1);
        for(int i=n-1;i>=0;i--)     //从后往前搜索
            res[i]=query(a[i]+1,1);
        for(int i=0;i<n;i++)
            printf("%d\n",res[i]);
    }
    return 0;
}

 

 

你可能感兴趣的:(poj)