POJ2182 Lost Cows

Posted autoint

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2182 Lost Cows相关的知识,希望对你有一定的参考价值。

题意

Language:
Lost Cows
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 13448Accepted: 8559

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

分析

从后往前考虑,如果第(k)头牛前面有(A_k)头比它矮,那么它的身高(H_k)是数值(1sim n)中第(A_k+1)小没有在({H_{k+1},H_{k+2},dots,H_n})中出现的数。

那么用01树状数组维护,每次倍增求第(A_k+1)小的就行了。

时间复杂度(O(n log n)),USACO的数据是真的弱,n才8000。

代码

#include<iostream>
#include<cmath>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
    while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
    return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;

co int N=8e3+1;
int n,t,a[N],c[N],h[N],p[14];
void add(int x){
    while(x<=n) --c[x],x+=x&-x;
}
int main(){
//  freopen(".in","r",stdin),freopen(".out","w",stdout);
    p[0]=1;
    for(int i=1;i<14;++i) p[i]=p[i-1]<<1;
    t=log((float)read(n))/log(2.0);
    for(int i=1;i<=n;++i){
        ++c[i];
        if(i+(i&-i)<=n) c[i+(i&-i)]+=c[i];
    }
    a[1]=1;
    for(int i=2;i<=n;++i) a[i]=read<int>()+1;
    for(int i=n,ans,sum;i;--i){
        ans=sum=0;
        for(int j=t;j>=0;--j)
            if(ans+p[j]<=n&&sum+c[ans+p[j]]<a[i])
                sum+=c[ans+p[j]],ans+=p[j];
        add(h[i]=ans+1);
    }
    for(int i=1;i<=n;++i) printf("%d
",h[i]);
    return 0;
}








以上是关于POJ2182 Lost Cows的主要内容,如果未能解决你的问题,请参考以下文章

POJ2182 Lost Cows

POJ2182Lost Cows

POJ 2182 Lost Cows 巧妙解法

POJ2182 Lost Cows (树状数组+二分)

POJ2182:Lost Cows

18.10.15 POJ 2182 Lost Cows(线段树)