hdu-5493 Queue (binary + Tree array)

Source: Internet
Author: User

Topic Links:

Queue

Time limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1093 Accepted Submission (s): 566


Problem DescriptionNPeople numbered from 1 toNIs waiting in a bank for service. They all stand in a queue, but the queue never moves. It is lunch time now, so they decide to go out and has lunch first. When they get back, they don ' t remember the exact order of the queue. Fortunately, there is some clues.
Every person has a unique height, and we denote the height of theI-th Person AShI . TheI-th person remembers that there werekI People who stand before him and is taller than him. Ideally, the enough to determine the original order of the queue uniquely. However, as they were waiting for too long, some of them get dizzy and countedkI In a wrong direction.kI could is either the number of taller people before or after the i-th person.
Can them to determine the original order of the queue?

Inputthe first line of input contains a numberTindicating the number of test cases (T≤ ).
Each test case starts with a line containing an integerNindicating the number of people in the queue (1≤N≤100000 ). Each of the nextNLines consists of the integershI andkI As described above (1 ≤ Hi≤109,0≤< Span id= "mathjax-span-81" class= "Msubsup" >k i≤ n− 1 ). Note that the order of the givenhI andki is randomly shuffled.
The sum of N over all test cases would not exceed 6

Outputfor each test case, output a single line consisting of "Case #X: S". X is the test Case number starting from 1. S is People's heights in the restored queue, separated by spaces. The solution may is unique, so you have need to output the smallest one in lexicographical order. If It is impossible to restore the queue, you should output "impossible" instead.

Sample Input3310 120 130 0310 020 130 0310 020 030 1

Sample outputcase #1:30Case #2:30Case #3: Impossible

Test Instructions:

Give the height of the n person, and how many taller the person is in front or in the back, and now let you find the height of the queue with the smallest dictionary order;

Ideas:

Sorting can get the total number of people in this queue higher than the assumption is num, if num<k than the total number of words will not be possible;

Then we turn this k into a higher number in front of her, this k is min (k,num-k) so that the dictionary order is minimal;

Then it is to ask for the height of each position, the height from the beginning of the order, this is equivalent to give a sequence of reverse number, and then let you restore the sequence;

From small to large greedy, for each person two points her position, determined after updating to the tree array, the complexity O (n*log2n);

AC Code:

#include <bits/stdc++.h>using namespace std; typedef long LONG Ll;const int maxn=1e5+10;int n,ans[maxn],sum[maxn],hi[maxn];struct node{int h,k,id;} Po[maxn];int CMP (node A,node b) {return a.h>b.h;}    int CMP1 (node A,node b) {if (a.h==b.h) return a.k<b.k; return a.h<b.h;} inline int lowbit (int x) {return x& (-X);}        inline void Update (int x) {while (x<=n) {sum[x]++;    X+=lowbit (x);    }}inline int query (int x) {int s=0;        while (x) {s+=sum[x];    X-=lowbit (x); } return s;}    int main () {int t,case=0;    scanf ("%d", &t);        while (t--) {printf ("Case #%d:", ++case);        scanf ("%d", &n);        for (int i=1;i<=n;i++) scanf ("%d%d", &AMP;PO[I].H,&AMP;PO[I].K), po[i].id=i;        Sort (po+1,po+n+1,cmp);        int flag=0,num=0;po[0].h=-1;            for (int i=1;i<=n;i++) {if (po[i].h!=po[i-1].h) num=i-1;            if (po[i].k>num) {flag=1;break;}        Hi[i]=num; } if (flag) PRintf ("impossible\n");            else {for (int i=1;i<=n;i++) po[i].k=min (PO[I].K,HI[I]-PO[I].K), sum[i]=0;            Sort (PO+1,PO+N+1,CMP1);                for (int i=1;i<=n;i++) {int l=po[i].k+1,r=n;                    while (l<=r) {int mid= (L+R) >>1;                    if (Mid-query (mid) >po[i].k) r=mid-1;                else l=mid+1;                } ans[r+1]=po[i].h;            Update (R+1);            } for (int i=1;i<=n;i++) printf ("%d", ans[i]);        printf ("\ n");  }} return 0;   }

  

hdu-5493 Queue (binary + Tree array)

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.