Lost cows
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 9477 |
|
Accepted: 6110 |
Description
N (2 <= n <= 8,000) cows has 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 be Fore Dinner. When it was time-to-line up for their evening meal, they do not line up in the required ascending numerical order of thei R brands.
Regrettably, FJ does not has a-to sort them. Furthermore, he ' s not very good at observing problems. Instead of writing cow ' s brand, he determined a rather silly statistic:for each cow on line, he knows the Numbe R of cows that precede that cow on line that does, in fact, has 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 has brands smaller than th At cow. Of course, no cows precede the first cow in line, so she was not listed. Line 2 of the input describes the number of preceding cows whose brands is smaller than the cow in slot #2; Line 3 describes the number of preceding cows whose brands is 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 on line; Line 2 tells the brand of the second cow; And so on.
Sample Input
51210
Sample Output
24531
Given the number of n and each number before it is smaller, there are several need to output the original sequence
The true value of the last number is a[n]+1
Delete a[n]+1 in sequence, update a[i], then the true value of N-1 number is a[n-1]+1
Two points to find the value to delete
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int bit[8888],ans[8888] ; int a[8888],n;int lowbit (int x) { return x& (-X);} int getsum (int x) { int cnt=0; while (x>0) {cnt+=bit[x];x-=lowbit (x); } return CNT;} void Add (int x,int a) { while (x<8888) {bit[x]+=a;x+=lowbit (x); }} int bin_find (int x) { int l=1,r=n,m; while (l<=r) {m= (l+r) >>1;int k=getsum (m); if (k>=x) r=m-1;else l=m+1; } while (Getsum (m) <x) m++; return m;} int main () { while (scanf ("%d", &n)!=eof) {a[1]=0;for (int i=2;i<=n;i++) scanf ("%d", &a[i]); memset (bit,0,sizeof (bit)), for (int i=1;i<=n;i++) Add (i,1), for (int i=n;i>=1;i--) { Ans[i]=bin_find ( A[I]+1); Add (ans[i],-1);} for (int i=1;i<=n;i++) printf ("%d\n", Ans[i]); } return 0;}
POJ 2182 Lost Cows tree-like array