hdu 5493
来源:互联网 发布:在淘宝上买steam游戏 编辑:程序博客网 时间:2024/06/05 04:54
Queue
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 348 Accepted Submission(s): 194
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
解法: 对于一个人来说, 如果他有k个比他大的在左边或者右边, 那么有两种情况, 一种是在左边, 那么就是留k个空位, 还有一种是在右边, 那么就是在左边留 n - i - k个空位. 因为要字典序最小, 所以取最小就可以了。
code:
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <cmath>using namespace std;#define rep(i,a,n) for (int i=a;i<=n;i++)#define per(i,n,a) for (int i=n;i>=a;i--)#define pb push_back#define mp make_pair#define all(x) (x).begin(),(x).end()#define SZ(x) ((int)(x).size())#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;ll mod=1000000007;ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}#define Fast_IO ios_base::sync_with_stdio(0);cin.tie(0)int T;int n;PII a[100100];int c[100100];void add(int pos, int val){ while(pos<=n){ c[pos] += val; pos += (pos&(-pos)); }}int sum(int pos){ int res = 0; while(pos>=1){ res += c[pos]; pos -= (pos&(-pos)); } return res;}int ans[100100];int getpos(int k){ int l = 1; int r = n; int mid; int ans; while(l <= r){ mid = (r+l)>>1; if(sum(mid) < k){ l = mid + 1; } else{ r = mid-1; ans = mid; } } return ans;}int main(){ cin>>T; int icase = 0; while(T--){ cin>>n; rep(i, 1, n) scanf("%d %d", &a[i].fi, &a[i].se); sort(a+1, a+n+1); memset(c, 0, sizeof(c)); for(int i=1; i<=n; i++) add(i, 1); bool flag = true; for(int i=1; i<=n; i++){ int tmp = min(n - (a[i].se + i), a[i].se); if(tmp < 0){ flag = false; break; } tmp++; int pos = getpos(tmp); add(pos, -1); ans[pos] = a[i].fi; } printf("Case #%d: ", ++icase); if(!flag) printf("impossible\n"); else{ rep(i, 1, n) printf("%d%s", ans[i], i==n?"\n":" "); } } return 0;}
0 0
- hdu 5493
- hdu 5493(线段树)
- HDU 5493 Queue
- hdu 5493(线段树)
- HDU 5493 线段树
- HDU 5493 线段树
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- 并查集(Union-Find)算法介绍
- hdoj Constructing Roads In JGShining's Kingdom (LIS变形+二分)
- Netcat简介
- anchorPoint,position,frame 深刻理解
- Regionals 2007 >> Asia - Nanjing - "Ray, Pass me the dishes!" 线段树 难 uva live 3938
- hdu 5493
- 一道小学生题目:红星+星红+星红=爱星星,问:爱、红、星各代表数字几?
- dede 列表页 上一页下一页 只要链接
- 如何删除微软拼音输入法2007
- 在fragment中添加上下文菜单
- Spring annotation 之lookup (方法注入)
- MFC消息机制逆向追踪
- 用jquery动画实现的简单的图片轮播
- textview属性