您的位置:首页 > 其它

poj 2182 Lost Cows (单点线段树)

2016-11-01 20:31 399 查看
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

题意:给出的数字表示从第2到第n只牛左边有几个比它序号小的牛,要求每个位置的牛的编号是多少
思路:线段树存储的是每个区间内还有几个空位,我们在逆序插入的时候判段是否满足</span></span>




代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=8005;
int str[3*N],s
,w
;
void build(int l,int r,int n)
{
if(l==r)
{
str
=1;
return;
}
int temp=(l+r)/2;
build(l,temp,2*n);
build(temp+1,r,2*n+1);
str
=str[2*n]+str[2*n+1];
}
void update(int wi,int pos,int l,int r,int n)
{
if(l==r)
{
str
=0;
w[pos]=l;
return;
}
int temp=(l+r)/2;
if(wi<str[2*n])
update(wi,pos,l,temp,2*n);
else
update(wi-str[2*n],pos,temp+1,r,2*n+1);
str
=str[2*n]+str[2*n+1];
}
int main()
{
int n;
while(~scanf("%d",&n))
{
build(1,n,1);
for(int i=2;i<=n;i++)
scanf("%d",&s[i]);
memset(w,0,sizeof(w));
for(int t=n;t>=1;t--)
update(s[t],t,1,n,1);
for(int i=1;i<=n;i++)
printf("%d\n",w[i]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: