poj 2182 Lost Cows

Lost Cows

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7310   Accepted: 4658

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

USACO 2003 U S Open Orange
/*

题意: 有n个数,从1到n,打乱顺序,
现输入n-1个数,第i个数表示序列中第1到i-1的数比第i个数小的个数.要求输出该序列
输入: 5 1 2 1 0
输出: 2 4 5 3 1

*/

#include <iostream> //线段树
using namespace std;
struct node
{
int l,r,num; //num记录[l,r]中还剩余多少人尚未被确定
}tree[40000];
void build(int n,int s,int t)
{
tree[n].l=s;
tree[n].r=t;
tree[n].num=t-s+1;
if(s<t)
{
int mid=(s+t)>>1;
build(2*n,s,mid);
build(2*n+1,mid+1,t);
}
}
int query(int i,int id) //除掉那些已经确定下来的数,现在排在第 id 位的数
{
while( tree[i].l != tree[i].r )
{
tree[i].num--;
if(tree[2*i].num>=id)
i=2*i;
else
{
id-=tree[2*i].num;
i=2*i+1;
}
}
tree[i].num=0; //到达叶子节点
return tree[i].l;
}
int arr[8010],ans[8010];
int main()
{
int n,i;
cin>>n;
build(1,1,n);
for(i=1;i<n;++i) //arr[0]值一直都是 0
cin>>arr[i];
for(i=n-1;i>=0;--i) //从后往前推导
{
ans[i]=query(1,arr[i]+1); //从根结点开始查找
}
for(i=0;i<n;++i)
cout<<ans[i]<<endl;
return 0;
}
 
 
 
 
posted @ 2012-07-25 11:16  jiai  Views(325)  Comments(0Edit  收藏  举报