HDU 5493 Queue

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

2015 ACM/ICPC Asia Regional Hefei Online

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

 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 }

时间: 2024-11-05 18:09:59

HDU 5493 Queue的相关文章

HDU 5493 Queue 树状数组

Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 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,

【线段树】HDU 5493 Queue (2015 ACM/ICPC Asia Regional Hefei Online)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5493 题目大意: N个人,每个人有一个唯一的高度h,还有一个排名r,表示它前面或后面比它高的人的个数,求按身高字典序最小同时满足排名的身高排列. 题目思路: [线段树] 首先可以知道,一个人前面或后面有r个人比他高,那么他是第r+1高或第n-i-r+1高,i为这个人是第几高的. 所以先将人按照身高从小到大排序,接下来,把当前这个人放在第k=min(r+1,n-i-r+1)高的位置. 用线段树维护包

hdu 5493 Queue treap实现将元素快速插入到第i个位置

input T 1<=T<=1000 n 1<=n<=100000 h1 k1 h2 k2 ... ... hn kn 1<=hi<=1e9  0<=ki<=n-1 sum(n)<=1e6 hi!=hj(i!=j) output hi指第i个人的身高,ki指这个人前面或者后面比他高的人数 Case #cas: 输出可能的最小序列,没有输出impossible 做法:将所有人按身高排序,从高到低插入数组中,则插入到第i个人时,数组里所有人都比他高,用tr

hdu 4441 Queue Sequence(splay)

题目链接:hdu 4441 Queue Sequence 这题看了题解写的,题解传送门 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 #define ls l,m,rt<<1 4 #define rs m+1,r,rt<<1|1 5 using namespace std; 6 typedef long long ll; 7 8 const int N=1e6+7; 9 i

Hdu 5493 合肥网络赛 1010 Queue

在线求第k大,第一次用二分+树状数组写...比赛的时候分治啊,splay啊,主席树啊换来换去,然而以前为什么不知道可以这么写... 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 #include <vector> 7 #include <set> 8 #de

HDU 4441 Queue Sequence(splay)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4441 题意:一个数列,三种操作:(1)插入:找到没在当前数列中的最小的正整数i,将其插在位置p之后,并将-i插入某个位置使得满足先进先出(i表示进,-i表示出),这个位置尽量靠右:(2)删除:删掉数字i以及-i:(3)询问:查询i和-i之间的数字的和. 思路:对于没在数列中的数字可以用一个set直接维护.i的插入是正常的splay操作.对于-i的插入,我们首先找到i之前有几个正数,比如有x个,那么-

补题列表

上海网络赛: HDU 5468 Puzzled Elena HDU 5469 Antonidas HDU 5473 There was a kingdom 合肥网络赛: HDU 5487 Difference of Languages HDU 5486 Difference of Clustering HDU 5489 Removed Interval HDU 5492 Find a path HDU 5493 Queue 弱校联萌Day1: B. Carries D. Vertex Cover

HDU 1908 Double Queue&lt;Set&gt;

Problem Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of th

hdu 1972.Printer Queue 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1972 题目意思:需要模拟打印机打印.打印机里面有一些 job,每个job被赋予1-9的其中一个值,越大表示优先级越高,越早被打印.job这个队列是会向前推进的,如果排在最前面的job优先级最高,那么才打印,否则就把这个job放到队列最后.问给出 m 这个位置的job要经过多长时间才被打印. 规定每次打印时间为一分钟,移动 job到队列最后不占时间. 练开优先队列就继续吧---不过这题不是咯. 仅仅用