hdu 5493 Queue 线段树加二分

来源:互联网 发布:淘宝经典文案 编辑:程序博客网 时间:2024/05/21 13:58

题意:

给你n个人的身高和这n个人满足在他前面有k个人比他高或者在他后面有k个人比他高。问是否存在这样的序列,如果有输出字典序最小的排列(输出身高)。对于一个位置上放什么身高的人我们可以优先放身高小的,以由小到大逐个考虑,假设现在考虑第i个元素,有num个人要比他高,由于他前面的人都比他矮,因此可以全部忽略。这时候因为要考虑2种情况,那么我们优先选择会让第i个元素比较靠前的策略。如果是前面有num个人比他高,那么第i个人就在当前队列的第num+1个位置;如果后面有num个人比他高,由于当前队列长度缩减为了n-i+1,那么第i个元素在当前队列的位置就是(n-i+1)-num。然后取两者中的较小者即可。如果发现结果小于1,那么无解。

ACcode:


#include <bits/stdc++.h>#define maxn 100005#define m ((l+r)>>1)#define tmp (st<<1)#define lson l,m,tmp#define rson m+1,r,tmp|1using namespace std;struct N{    int h,num;}my[maxn];bool cmp(N a,N b){    return a.h<b.h;}int t[maxn<<2],ans[maxn];void build(int l,int r,int st){    t[st]=r-l+1;    if(l==r)return ;    build(lson);    build(rson);}void update(int a,int b,int l,int r,int st){    --t[st];    if(l==r){        ans[l]=b;        return ;    }    if(t[tmp]>a) update(a,b,lson);    else update(a-t[tmp],b,rson);}int main(){    int loop,n,cnt=1;    scanf("%d",&loop);    while(loop--){        scanf("%d",&n);s        for(int i=1;i<=n;++i)scanf("%d%d",&my[i].h,&my[i].num);        sort(my+1,my+1+n,cmp);        build(1,n,1);        bool flag=true;        for(int i=1;i<=n&&flag;++i){            int a=my[i].num,b=my[i].h;            a=min(a,n-i-a);            if(a<0)flag=false;            else update(a,b,1,n,1);        }        printf("Case #%d: ", cnt++);        if(!flag)puts("impossible");        else for(int i=1;i<=n;++i)printf("%d%c",ans[i],i==n?'\n':' ');    }    return 0;}


0 0