HDU5493-Queue
来源:互联网 发布:快手免费刷播放软件 编辑:程序博客网 时间:2024/06/05 04:20
Queue
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1177 Accepted Submission(s): 608
Problem Description
Every person has a unique height, and we denote the height of the
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≤1000 ).
Each test case starts with a line containing an integerN indicating the number of people in the queue (1≤N≤100000 ). Each of the next N lines consists of two integers hi and ki as described above (1≤hi≤109,0≤ki≤N−1 ). Note that the order of the given hi and ki is randomly shuffled.
The sum ofN over all test cases will not exceed 106
Each test case starts with a line containing an integer
The sum of
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
3310 120 130 0310 020 130 0310 020 030 1
Sample Output
Case #1: 20 10 30Case #2: 10 20 30Case #3: impossible
Source
2015 ACM/ICPC Asia Regional Hefei Online
Recommend
wange2014
解题思路:二分+树状数组,可以先按照身高从小到大排序,假设当前到了第i高的人,他前面或者后面有k个人,那么他前面的所有人都比他矮,比他高的还有n-i个人,因为要求字典序最小,所以他在未排的人中的位置 p = min(k, n - i - k)。每个人有两个可能位置,因为他之前的都比他矮,那么为了让字典序最小,就选择一个较小的位置。当n - i - k < 0 时,说明没有多余空格,那么无解。二分第i个人的位置, 用树状数组判断他前面有多少人以此确定空余的位置数。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;struct node{int h, k;}a[100009];int c[100009];int ans[100009];int n;int lowbit(int x){return x&-x;}void add(int x,int val){while (x <= n){c[x]+=val;x += lowbit(x);}}int getsum(int x){int sum = 0;while (x){sum += c[x];x -= lowbit(x);}return sum;}bool cmp(node a, node b){return a.h < b.h;}int main(){int t, cas = 0;scanf("%d", &t);while (t--){scanf("%d", &n);for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].h, &a[i].k);memset(c, 0, sizeof c);sort(a + 1, a + 1 + n, cmp);for (int i = 1; i <= n; i++) add(i,1);int flag = 1;for (int i = 1; i <= n; i++){int k = n - i,kk=a[i].k;if (kk > k) { flag = 0; break; }int temp = min(kk+1,k-kk+1);int l = 1, r = n,ans1;while (l <= r){int mid = (l + r) >> 1;if (getsum(mid) >= temp) { ans1 = mid, r = mid - 1; }else l = mid + 1;}ans[ans1] = a[i].h;add(ans1, -1);}if (!flag) printf("Case #%d: impossible\n",++cas);else{printf("Case #%d:", ++cas);for (int i = 1; i <= n; i++) printf(" %d", ans[i]);printf("\n");}}return 0;}
0 0
- hdu5493 Queue
- HDU5493-Queue
- HDU5493 Queue(线段树)
- hdu5493 Queue 线段树
- HDU5493 Queue【线段树】
- HDU5493 Queue(线段树)
- HDU5493 Queue(线段树)
- HDU5493 Queue 线段树单点更新
- hdu5493
- hdu5493 树状数组+二分
- hdu5493 线段树
- Queue
- queue
- Queue
- QUEUE ~
- queue
- queue
- queue
- 中兴捧月比赛DIJKSTRA派算法说明
- 各种内部排序算法的比较和应用
- Notification
- 漫步数理统计二十八——混合分布
- Javascript中canvas绘制五子棋棋盘
- HDU5493-Queue
- poj1509:Glass Beads(后缀自动机)
- 2016搜索提高1017
- SQL中常用模糊查询的四种匹配模式&&正则匹配
- python中的range和xrange的使用和区别
- Spring4 之IOC详解xml配置
- 论如何获取洛谷所有用户的ID
- 详解Javascript 函数声明和函数表达式的区别
- 随便写写