POJ 2182 Lost Cows (cattle sorting, line segment tree), poj2182

Source: Internet
Author: User

POJ 2182 Lost Cows (cattle sorting, line segment tree), poj2182

Language:DefaultLost Cows
Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:9207   Accepted:5922

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


Sort the ox, The nheaded ox, and then start from the second ox to give him the position of so many cows smaller than his serial number, and finally determine the ranking of the ox


For the first time, I was shocked when I saw other people's questions. f [pos]. va --, I could even leave this position out and won't be accessed in the future. Another point is that I will rank the ox from the back to the back.


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)using namespace std;#define N  8005struct stud{int le,ri;int len;}f[N*4];int a[N];int ans[N];void build(int pos,int le,int ri){    f[pos].le=le;    f[pos].ri=ri;    f[pos].len=ri-le+1;    if(ri==le)  return ;    int mid=MID(le,ri);    build(L(pos),le,mid);    build(R(pos),mid+1,ri);}int query(int pos,int le){   f[pos].len--;   if(f[pos].le==f[pos].ri)      return f[pos].le;   if(f[L(pos)].len>=le)      return query(L(pos),le);   return query(R(pos),le-f[L(pos)].len);}int main(){    int i,j,n;    while(~scanf("%d",&n))    {        build(1,1,n);        for(i=2;i<=n;i++)            scanf("%d",&a[i]);        a[1]=0;        for(i=n;i>=1;i--)            ans[i]=query(1,a[i]+1);        for(i=1;i<=n;i++)            printf("%d\n",ans[i]);        return 0;    }    return 0;}










Who has the correspondence between data structure algorithms and POJ questions?

An asterisk indicates a typical question or a better algorithm.

1195 Mobile phones tree Array
1455
1521 Entropy huffman
1703 Find them, Catch them and query the set
1785 Binary Search Heap Construction
1794 Castle Wils Reverse Order
1961 Period KMP recurrence factor
1984 * Navigation Nightmare and check the set + coordinate translation
1986 * Distance Queries LCA
1988 * Cube Stacking and query set applications
1990 * MooFest line segment tree
2010 * Moo University-Financial Aid Max heap-min heap
2182 Lost Cows line segment tree
2183 Bovine Math Geniuses hash
2188 Cow Laundry Reverse Order
2227 The Wedding Juicer heap + floodfill
2236 Wireless Network and query set
2266 * Quadtree Recursion
2269 * Friends expression
2270 Quadtree II or: Florida Jones strikes back will be 2266 vice versa
2299 Ultra-QuickSort Merge Sorting
2352 Stars tree Array
2395 Out of Hay and query set
2482 Stars in Your Window static 2 Cross Tree
2513 Colored Sticks and check the set
2524 Ubiquitous Religions and query set
2528 Mayor's posters line segment tree
2567 Code the Tree
2750 * Potted Flower line segment tree
2777 Count Color line segment tree
2796 Feel Good RMQ
2823 Sliding Window heap or double-end queue
2828 Buy Tickets line segment tree
2886 * Who Gets the Most Candies? Line Segment tree
2892 * Tunnel Warfare tree Array
3214 * Heap post-order traversal. Each node deducts the corresponding sub-guarantee attribute, and then calculates the longest non-descending sequence for the traversal result.
3253 Fence Repair huffman
3263 Tallest Cow line segment tree
3274 * Gold Balanced Lineup hash
3277 City Horizon line segment tree
3320 Jessica's Reading Problem queue operation or minimum heap
3321 * Apple Tree Array
3332 Parsing Real Numbers DFA
3344 Chessboard Dance queue Simulation
3349 Snowflake Snow Snowflakes hash (or brute force)
3437 Tree Grafting dfs Tree construction
3461 Oulipo KMP
3468 A Simple Problem with Integers segment tree Interval Update, lazy operation
3631 Cuckoo Hashing and query Sets
3667 Hotel line segment tree
3690 Constellations trie match
3695 Rectangles matrix cutting... remaining full text>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.