POJ 2828 Buy Tickets "ticket queue to find position output final position sequence + segment tree"

Source: Internet
Author: User

Title Address: http://poj.org/problem?id=2828

Sample Input

40 771 511 332 6940 205231 192431 38900 31492

Sample Output

77 33 69 5131492 20523 3890 19243

Hint

The figure below shows how the Little Cat found out the final order of people in the queue described in the first Test CAs E of the sample input.

The problem is this: there are now n people to buy tickets, but in the dark can cut the queue. Give the data of the N individuals who will buy tickets in turn. Contains two items: POS, at the moment when the man comes, he must
Insert to pos this position, if the current POS no one, it is best, directly insert him. But if the POS is in the position of someone, in a realistic sense, the first person inserted after, the equivalent of his The back of the people on the original basis of all moved backward a position! (PS: That's why people hate jumping in the queue!!!) Each person carries a Val value when all n individuals are determined under to output a Val sequence afterwards.
Analysis: It is easy to think that the person in front of you, although the first arrived, but his position is not sure, because later people can jump in line to your position, and you then moved a position. That is to say,
when we determine the final sequence, we cannot process it in the order of first served. We should deal with it from the back! The position of the last person to be reached must be definite, and his position is definitely at once. Similarly, forward recursion processing. algorithm structure: Using the segment tree, each node is stored in the current interval of the remaining empty position.
      Note A place: line tree subscript is starting from 1, and those who buy tickets to the queue is starting from 0, so it should be in the search for the update pos to add 1, if not add 1 is to determine which child node It's >pos when it's moving. Code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < math.h> #include <iostream> #include <algorithm>using namespace std;struct seq{int pos, Val;}    A[200000+10];int num[800000+100];int ans[200000+100];void Build (int rt, int ll, int rr) {num[rt]=rr-ll+1;    if (LL==RR) return;//has reached the root node Build (rt*2, LL, (LL+RR)/2); Build (Rt*2+1, (LL+RR)/2+1, RR);} int update (int pos, int rt, int ll, int rr) {num[rt]--;//empty position number-1 if (LL==RR) return ll;//reach root node if (num[rt*2]    >pos)//pos is starting from 0 and the line segment tree is stored from 1 but 0 and 1 in this topic are the corresponding storage return update (POS, rt*2, LL, (LL+RR)/2); else return update (pos-num[rt*2], rt*2+1, (LL+RR)/2+1, RR);}    int main () {int n, I; while (scanf ("%d", &n)!=eof) {for (i=0; i<n; i++) {scanf ("%d%d", &a[i].pos, &a[i].val)        ; }//the Table save Build (1, 1, N),//start the range from 1th node [1,n]/* for (i=1; i<=7; i++) printf ("%d---", num[i]); */for (i=n-1; i>=0; i--) {ans[update (A[i].pos, 1, 1, n)]=a[i].val; } for (I=1; i<=n; i++) {printf ("%d%c", Ans[i], i==n? '        \ n ': '); }} return 0;}

POJ 2828 Buy Tickets "ticket queue to find position output final position sequence + segment tree"

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.