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
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 (T1000).
Each test case starts with a line containing an integer N indicating the number of people in the queue (1N100000). Each of the next N lines consists of two integers hi and ki as described above (1hi109,0kiN1). Note that the order of the given hi and ki is randomly shuffled.
The sum of N over all test cases will not exceed 106
 

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
原创粉丝点击