Lost Cows
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7984 | Accepted: 5087 |
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
树状数组:
/*
首先对于样例5 1 2 1 0,逆序是0 1 2 1,那么从左到右
分析,0那个位置肯定是1,因为1是最小的,前面不可能有比它
小的。那么1此时应该是3,因为1已经在他后面了,所以在他前面的
肯定是2。以此类推。
树状数组维护第i号奶牛之前已经使用了多少个位置getSum(i)。
那么空余的位置就是i-getSum(i);如果空余位置恰好等于f[j]+1,
说明i就是当前位置的编号。其中f[j]是处于位置j的奶牛,在它前面比它小的
奶牛个数,+1是因为还要包括它自己。
280K 16MS
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define SIZE 8888
using namespace std;
int N;
int f[SIZE];
int ans[SIZE];
int sum[SIZE];
int lowbit(int x)
{
return x & (-x);
}
int getSum(int x)
{
int ret = 0;
for(int i=x; i>0; i-=lowbit(i))
ret += sum[i];
return ret;
}
void update(int x,int v)
{
for(int i=x; i<=N; i+=lowbit(i))
sum[i] += v;
}
int binarySearch(int v)
{
int low = 1, high = N;
while(low <= high)
{
int mid = (low + high) >> 1;
int temp = mid - getSum(mid);
if(temp < v)
low = mid + 1;
else
high = mid - 1;
}
return low;
}
int main()
{
scanf("%d",&N);
for(int i=2; i<=N; i++)
{
scanf("%d",&f[i]);
sum[i] = 0;
}
f[1] = 0;
for(int i=N; i>=1; i--)
{
ans[i] = binarySearch(f[i]+1);
update(ans[i],1);
}
for(int i=1; i<=N; i++)
printf("%d\n",ans[i]);
return 0;
}
线段树:
424K 32MS #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define MAX 8005 using namespace std; struct node { int left,right,empty; }; int N; node seg[MAX<<2]; int tar[MAX]; int ans[MAX]; void create(int l,int r,int idx) { seg[idx].left=l; seg[idx].right=r; seg[idx].empty=r-l+1; if(l==r) return; int mid=(l+r)>>1; create(l,mid,2*idx); create(mid+1,r,2*idx+1); } void query(int tar,int pos,int idx) { seg[idx].empty --; if(seg[idx].left == seg[idx].right) { ans[pos]=seg[idx].left; return; } if(tar<=seg[2*idx].empty) query(tar,pos,2*idx); else query(tar-seg[2*idx].empty,pos,2*idx+1); } int main() { scanf("%d",&N); for(int i=2;i<=N;i++) scanf("%d",&tar[i]); tar[1]=0; create(1,N,1); for(int i=N;i>=1;i--) { query(tar[i]+1,i,1); } for(int i=1;i<=N;i++) printf("%d\n",ans[i]); return 0; }