Lost Cows
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11625 | Accepted: 7472 |
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.
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.
* 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
题意:FJ有n头牛,编号为1~n,它们并没有按照编号的顺序排好队列。现在,FJ只知道每一个牛前面有多少只牛的编号比它大。问你能不能判断出所有牛的编号。
思路:线段树。关键:每次最后一只牛的编号是可以确定的,即为pre[i]+1,将其编号从所有牛中删除,则倒数第二只牛的编号又可以确定为pei[i]+1,依此类推。
暴力就是最后一个牛的编号确定了,那么这个牛的位置就不能用了, 下一个最后的牛从最前面往后走(跨过确定位置的牛)。。这其实可以用线段树维护的,姿势就是下面代码。。线段树把每一个小区间都给维护的,删掉一个牛,这个区间还有他的孩子tree维护的值就--,然后二分找到她的位置。。线段树代码:
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 8005;
int tree[maxn*4];
void built(int root ,int l ,int r)
{
tree[root] = r - l + 1; //这个维护tree数组不是像取最大值或者求和那样要根据子节点慢慢回溯得到的,根据他的左右区间就知道了
if(l == r) return; //这里就不用再tree[root] =1了因为一进来,他就维护了tree数组 //所以不用放在两个built之后
int m = (l+r)/2;
built(root*2 ,l,m);
built(root*2+1,m+1,r);
}
int query(int root,int l ,int r ,int u)
{
tree[root]--; //插入一个 这个区间就-1
if(l == r ) return l;
int m =(l+r)/2;
if(u <= tree[root*2] ) query(root*2,l,m,u); //这里有点二分的意思。。
else query(root*2+1,m+1,r,u-tree[root*2]); //正如前面所说,这里没有通过回溯怎样,找到u在的位置就行,而且想通过回溯必须是return
}
int main()
{
int n , cow[maxn],dir[maxn];
cin >> n;
built(1,1,n);
cow[1] = 0;
for(int i = 2 ;i <=n ; i++)
cin >> cow[i];
for(int i = n ;i >= 1;i--)
dir[i] = query(1,1,n,cow[i]+1);
for(int i = 1; i <= n; i++)
cout << dir[i] << endl;
return 0;
}
暴力代码:
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
int main()
{
int n , j,cow[8005] ={0}, book[8005] = {0} , b[8005] = {0};
cin >> n;
for(int i = 2 ; i <= n; i++) cin >> cow[i];
b[n] = cow[n] + 1;
book[b[n]] = 1;
for(int i = n - 1; i >= 1 ; i--)
{
int count = 0 ;
for(j = 1 ; j <= n - 1 ; j++)
{
if(book[j] == 0) count++;
if(count == cow[i] + 1) break;
}
b[i] = j;
book[j] =1;
}
for(int i = 1; i <= n ;i++)
cout << b[i] <<endl;
return 0;
}