Lost Cows
| Time Limit:1000 MS |
|
Memory Limit:65536 K |
| Total Submissions:8838 |
|
Accepted:5657 |
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 holle' and drank a few too describeers 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
51210
Sample Output
24531
Source
USACO 2003 u s Open Orange
This is my question about the virgin line tree. I feel that the application of the line tree is really wide. I haven't achieved that flexible application of the line tree yet, the application scope of a line segment tree is wider than that of a tree array. You can also use a tree array to solve this problem. The tree array and line segment tree must be well mastered. It is important to use them flexibly !!
I still have no sense of this kind of English question. I can't understand the question at first, and my English skills need to be improved. I just started to look at the sample input for this question and check five inputs, however, there are only four inputs that I didn't understand at first. In fact, this is an implicit condition. It is the first in the out-of-order sorting, and there are no elements in front of it, so there is no smaller front than it, so the first one is 0, so the sample is 5 (, 0). After reading the question for a long time, I finally understood the question for a long time, then, the starting point of the Line Segment tree is not found;
The question is: give you a column of cows in disorder, and give you a number smaller than the number of cows in front of each cow, calculate the number of each ox after the queue;
Based on the idea of the great gods, we first establish a line segment tree, and then scan from the back to the front. When we encounter sequence number I, it indicates that it is the I + 1 of the remaining sequence number, and then we use the line segment tree to find it, to check whether the number in a range can meet the requirement of the number to be searched to become I + 1, the left subtree can be recursive, And the right subtree cannot be recursive until the leaf node, the leaf node value is the original number.
The following is the ac code;
# Include
# Define N 8002int num [N], ans [N]; struct segment {int l, r, len;} s [N]; void build (int root, int l, int r) // create a tree {s [root]. l = l; s [root]. r = r; s [root]. len = r-l + 1; // The Line Segment (according to the question) if (l = r) return; build (2 * root, l, (l + r) /2); build (2 * root + 1, (l + r)/2 + 1, r);} int query (int root, int k) // find the line segment tree {s [root]. len --; // each time a search is performed, the interval is reduced by 1, indicating that the node is deleted after a number is found. if (s [root]. l = s [root]. r) return s [root]. l; // specifies the else if (k <= s [2 * root] of the leaf node. len) {return query (2 * root, k);} // recursive left subtree else {return query (2 * root + 1, k-s [2 * root]. len) ;}// recursive right subtree} int main () {int n; scanf ("% d", & n); for (int I = 2; I <= n; I ++) scanf ("% d", & num [I]); num [1] = 0; // The first number is 0; build (1, 1, n); for (int I = n; I> = 1; I --) // Reverse Order ans [I] = query (1, num [I] + 1); for (int I = 1; I <= n; I ++) printf ("% d \ n", ans [I]); return 0 ;}
This question can also be done using array arrays. In other words, we will do it tomorrow (not to be continued ..)