标签:
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
Sample Output
题解:奶牛的前面比自己小的数知道,但是需要从后面进行才能知道该奶牛在所有“奶牛”中的位置,最后一名奶牛的前面比他小的是所有奶牛,倒数第二前面比他小的指的是除去最后一头后的数。。。。。一次类推,线段树保存了该区间的奶牛数量。如果该奶牛在剩余奶牛中的排名小于等于某点的左区间,那么他的编号肯定在左端。否则,就可以得到他在右区间的位置。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int arr[400005];
int res[80004];
int a[80004];
void pushUp(int k)
{
arr[k] = arr[k << 1] + arr[(k << 1) | 1];
}
void segTree(int k,int l,int r)
{
if(l == r)
{
arr[k] = 1;
return;
}
int mid = (l + r) >> 1;
segTree(k << 1,l,mid);
segTree((k << 1) | 1,mid + 1,r);
pushUp(k);
}
void update(int k,int l,int r,int x,int y)
{
if(l == r)
{
res[x] = l;
arr[k] = 0;
return;
}
int mid = (l + r) >> 1;
if(y <= arr[k << 1])
{
update(k << 1,l,mid,x,y);
}
else
{
update((k << 1) | 1,mid + 1,r,x,y - arr[k << 1]);
}
pushUp(k);
}
int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
segTree(1,1,n);
a[1] = 0;
for(int i = 2;i <= n;i++)
{
scanf("%d",a + i);
}
for(int i = n;i > 0;i--)
{
update(1,1,n,i,a[i] + 1);
}
for(int i = 1;i <= n;i++)
{
printf("%d\n",res[i]);
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Lost Cows
标签:
原文地址:http://blog.csdn.net/wang2534499/article/details/47375903