Title: Lost Cows
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 9727 |
|
Accepted: 6261 |
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
Main topic:n different numbers (range 1-n) of the cattle, disorderly sequence in a row, it is known how many cows in front of each cow number is smaller than it, the sequence of the previous number of each cowIdeas:
Traversing the input sequence from the back forward, each value encountered by a means that the cow is in the remaining cattle in the a+1, delete this number, loop this process, the resulting sequence is the number of cows in this queue sequence. Find the position of the cows in the a+1 (numbered sort position) in the non-deleted number with the segment tree (reading order)
AC Code:
#include <stdio.h>int s[100000], ans[100000];struct seg{int L, R;int Len;} cow[100000];void Build (int v, int l, int r) {cow[v].l = L;COW[V].R = R;cow[v].len = R-l + 1;if (L = = r) {return;} int mid = (L + R)/2;build (v * 2, L, mid); Build (v * 2 + 1, mid + 1, r);} int query (int v, int k) {--cow[v].len;//length minus one if (cow[v].l = = COW[V].R) {//Find leaf node, note that Cow[v].len = = 0 is not used here, otherwise the single case will be returned directly, resulting in the failure to reach To the end return COW[V].R;} else if (Cow[v * 2].len >= k) {return query (V * 2, k);} Else{return Query (v * 2 + 1, k-cow[v * 2].len);}} int main () {int n;while (~scanf ("%d", &n)) {for (int i = 2; I <= n; ++i) {scanf ("%d", &s[i]);} S[1] = 0;build (1, 1, n);//From the back forward, the value of each s[i] indicates that the cow is in the remainder of the sequence of s[i] + 1for (int i = n; i >= 1; i) {ans[i] = query (1, s[i] + 1) ;} for (int i = 1; I <= n; ++i) {printf ("%d\n", Ans[i]);}}}
"POJ" 2182-lost cows "Getting Started with line segment trees"