【HDU - 5493】Queue(思维,贪心,线段树)
題干:
NN?people numbered from 1 to?NN?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?ii-th person as?hihi. The?ii-th person remembers that there were?kiki?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?kiki?in a wrong direction.?kiki?could be either the number of taller people before or after the?ii-th person.
Can you help them to determine the original order of the queue?
Input
The first line of input contains a number?TT?indicating the number of test cases (T≤1000T≤1000).
Each test case starts with a line containing an integer?NN?indicating the number of people in the queue (1≤N≤1000001≤N≤100000). Each of the next?NN?lines consists of two integers?hihi?and?kiki?as described above (1≤hi≤109,0≤ki≤N?11≤hi≤109,0≤ki≤N?1). Note that the order of the given?hihi?and?kiki?is randomly shuffled.
The sum of?NN?over all test cases will not exceed?106106
Output
For each test case, output a single line consisting of “Case #X: S”.?XX?is the test case number starting from 1.?SS?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 1Sample Output
Case #1: 20 10 30 Case #2: 10 20 30 Case #3: impossible題目大意:
有 N 個人排隊,每個人都忘記自己的位置。但是每個人都知道自己的Hi,Ki,Hi代表自己的身高,Ki代表自己前邊或后邊比自己高的人的個數。給你每個人的 Hi,Ki,求滿足題意的一組解。輸出每個位置上站著的人的身高,如果有多組解,輸出字典序最小的那一組。如果沒有滿足的情況,則輸出"impossible"。
解題報告:
首先按照身高從小到大排個序,依次決策位置,因為題目給出的是身高比當前人高的人的個數,所以這樣可以保證此時之前的決策對我自己沒啥影響,因為之前的決策都是身高比我矮的,關于這一點題干中并沒有約束。
考慮線段樹維護沒有被占的位置。這樣只需要在線段樹中查詢從前往后第K+1個不為0的位置,從后往前第K+1個不為0的位置,選最靠前的一個位置放上這個人即可。
這題按照身高從大到小排序也是可解的。思路類似,但是因為只知道相對位置不知道每個人的實際位置,所以需要平衡樹維護。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 1e5 + 5; struct Node{int h,id,k;bool operator<(const Node & b) const {return h < b.h;} } R[MAX]; struct TREE {int l,r,sum; } tr[MAX<<2]; void pushup(int cur) {tr[cur].sum = tr[cur*2].sum + tr[cur*2+1].sum; } void build(int l,int r,int cur) {tr[cur].l = l,tr[cur].r = r;if(l == r) {tr[cur].sum = 1;return;}int m = (l+r)>>1;build(l,m,cur*2);build(m+1,r,cur*2+1);pushup(cur); } int query(int k,int cur) {if(tr[cur].l == tr[cur].r) return tr[cur].l;if(tr[cur*2].sum >= k) return query(k,cur*2);else return query(k-tr[cur*2].sum,cur*2+1); } void update(int tar,int cur) {if(tr[cur].l == tr[cur].r) {tr[cur].sum = 0;return;}if(tar <= tr[cur*2].r) update(tar,cur*2);else update(tar,cur*2+1);pushup(cur); } int ans[MAX],n; int main() {int T,iCase=0;cin>>T;while(T--) {scanf("%d",&n);for(int i = 1; i<=n; i++) {scanf("%d%d",&R[i].h,&R[i].k);R[i].id = i;}sort(R+1,R+n+1); build(1,n,1);int flag = 1;for(int i = 1; i<=n; i++) {int res = tr[1].sum;if(R[i].k >= res) {flag = 0;break;}int tar = min(query(R[i].k+1,1),query(res-R[i].k,1));ans[tar] = R[i].h;update(tar,1);}printf("Case #%d:",++iCase);if(flag == 0) printf(" impossible");else for(int i = 1; i<=n; i++) printf(" %d",ans[i]);printf("\n");}return 0 ; }?
總結
以上是生活随笔為你收集整理的【HDU - 5493】Queue(思维,贪心,线段树)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: realpopup.exe - real
- 下一篇: 【csust】最小素因子问题(树状数组)