HDU 5493 Queue 树状数组+二分

Queue

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
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
 
 
题意:
 
  给你n个人,每个人都有不同的身高,
  告诉你这个人向前有k个人比他高,或者是向后有k个人比他高两者满足其一,
  问你是否能给出一个身高字典序最小的排列,
  能的话就输出
 
题解:
  
  答案要使身高字典序最小,
  我们以身高自小到大插入序列
  假设我们放第i个数在x位置,我们放之前,保证好要留多少个空位给后面n-i个都比他大的数
  这个二分位置就好,BIT维护前缀和
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+20, M = 30005, mod = 1e9+7, inf = 0x3f3f3f3f;
typedef long long ll;
//不同为1,相同为0

int n;
struct ss{int h,k;}a[N];
int cmp (ss s1,ss s2) {
    return s1.h<s2.h;
}
struct BIT {
    int tree[N] ;

    int lowbit(int x) {
        return x&(-x);
    }
    void add(int x, int add, int n) {
        for (; x <= n; x += lowbit(x)) {
            tree[x] += add;
        }
    }
    int sum(int x) {
        int s = 0;
        for (; x > 0; x -= lowbit(x)) {
            s += tree[x];
        }
        return s;
    }
    void clears() {
        memset(tree, 0, sizeof tree);
    }
}Bit;
int cal(int x) {
    int l = 1, r = n, ret = -1;
    while(l<=r) {
        int mid = (l+r)>>1;
        int G = mid - Bit.sum(mid);
        if(G>=x) r = mid-1,ret = mid;
        else l = mid+1;
    }
    return ret;
}
int main() {
    int T,cas = 1,ans[N];
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d%d",&a[i].h,&a[i].k);
        sort(a+1,a+n+1,cmp);
        Bit.clears();bool OK = true;
        for(int i=1;i<=n;i++) {
            int la = n - i;
            int k = a[i].k;
            if(a[i].k>la) {OK = false; break;}
            int p = (k<=la/2)? k:la-k;
            int ret = cal(p+1);
            Bit.add(ret,1,n);
            ans[ret] = a[i].h;
        }
        printf("Case #%d: ", cas++);
        if(!OK) puts("impossible");
        else {
            for(int i=1;i<n;i++) printf("%d ",ans[i]);
            printf("%d
",ans[n]);
        }
    }
    return 0;
}
  
原文地址:https://www.cnblogs.com/zxhl/p/5317604.html