码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5493 Queue

时间:2015-09-27 21:28:28      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

Queue

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 68    Accepted Submission(s): 40

Problem Description
N people numbered from 1 to N are 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 have lunch first. When they get back, they don’t remember the exact order of the queue. Fortunately, there are some clues that may help.
Every person has a unique height, and we denote the height of the i-th person as hi. The i-th person remembers that there were ki people who stand before him and are taller than him. Ideally, this is enough to determine the original order of the queue uniquely. However, as they were waiting for too long, some of them get dizzy and counted ki in a wrong direction. ki could be either the number of taller people before or after the i-th person.
Can you help them to determine the original order of the queue?

Input
The first line of input contains a number T indicating the number of test cases $(T\leq 1000)$.
Each test case starts with a line containing an integer N indicating the number of people in the queue $(1\leq N\leq 100000)$. Each of the next N lines consists of two integers hi and ki as described above $(1\leq h_i\leq 10^9,0\leq k_i\leq N−1)$. Note that the order of the given $h_i\ and\ k_i$ is randomly shuffled.
The sum of N over all test cases will not exceed $10^6$

Output
For 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 not be unique, so you only need to output the smallest one in lexicographical order. If it is impossible to restore the queue, you should output “impossible” instead.

Sample Input
3
3
10 1
20 1
30 0
3
10 0
20 1
30 0
3
10 0
20 0
30 1
 

 

Sample Output
Case #1: 20 10 30
Case #2: 10 20 30
Case #3: impossible
 

 

Source

 解题:贪心占位,按高度由低到高排序后,贪心占位就是了

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int INF = 0x3f3f3f3f;
 4 const int maxn = 100100;
 5 struct INFO {
 6     int height,k;
 7     bool operator<(const INFO &rhs)const {
 8         return height < rhs.height;
 9     }
10 } info[maxn];
11 struct node {
12     int lt,rt,val;
13 } tree[maxn<<2];
14 int ret[maxn];
15 inline void pushup(int v) {
16     tree[v].val = tree[v<<1].val + tree[v<<1|1].val;
17 }
18 void build(int lt,int rt,int v) {
19     tree[v].lt = lt;
20     tree[v].rt = rt;
21     if(lt == rt) {
22         tree[v].val = 1;
23         return;
24     }
25     int mid = (lt + rt)>>1;
26     build(lt,mid,v<<1);
27     build(mid + 1,rt,v<<1|1);
28     pushup(v);
29 }
30 void update(int pos,int v) {
31     if(tree[v].lt == tree[v].rt) {
32         tree[v].val = 0;
33         return;
34     }
35     if(pos <= tree[v<<1].rt) update(pos,v<<1);
36     else update(pos,v<<1|1);
37     pushup(v);
38 }
39 int query(int cnt,int v,bool ok) {
40     if(cnt > tree[v].val) return INF;
41     if(tree[v].lt == tree[v].rt) return tree[v].lt;
42     if(ok) {
43         if(tree[v<<1].val >= cnt) return query(cnt,v<<1,ok);
44         else return query(cnt - tree[v<<1].val,v<<1|1,ok);
45     }
46     if(tree[v<<1|1].val >= cnt) return query(cnt,v<<1|1,ok);
47     else return query(cnt - tree[v<<1|1].val,v<<1,ok);
48 }
49 int main() {
50     int kase,n,cs = 1;
51     scanf("%d",&kase);
52     while(kase--) {
53         scanf("%d",&n);
54         for(int i = 0; i < n; ++i)
55             scanf("%d%d",&info[i].height,&info[i].k);
56         sort(info,info + n);
57         bool flag = true;
58         build(1,n,1);
59         for(int i = 0; i < n && flag; ++i){
60             int L = query(info[i].k + 1,1,true);
61             int R = query(info[i].k + 1,1,false);
62             if(min(L,R) == INF) {
63                 flag = false;
64                 break;
65             }
66             update(min(L,R),1);
67             ret[min(L,R)] = info[i].height;
68         }
69         printf("Case #%d:",cs++);
70         if(flag){
71             for(int i = 1; i <= n; ++i)
72                 printf(" %d",ret[i]);
73             puts("");
74         }else puts(" impossible");
75     }
76     return 0;
77 }
View Code

 

HDU 5493 Queue

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4842723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!