Hdu 5493 Queue【伸展树/二分+树状数组】

队列重构问题
探讨了在给定每个人的高度和相对身高的线索下,如何确定银行排队的原始顺序。通过两种算法实现:一种利用伸展树维护序列,另一种采用树状数组结合二分查找的方法。

Queue

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1299    Accepted Submission(s): 654


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个人比他高,问是否存在这样的可行序列,如果存在,输出最小字典序的解,否则输出impossible.


思路1:


①首先我们将序列按照身高从大到小排序,那么当前排列到第i个人的时候,已知所有之前排列上的人都比他高,所以只要将这个人插入到合适的位子即可,并且这个位子我们尽可能的靠向左边。

②当且仅当k>i-1的时候无解。

③那么过程用伸展树维护一下即可。


附上队长Ac代码:


#include <bits/stdc++.h>
typedef long long int LL;
typedef unsigned long long int LLu;

using namespace std;

const int N =  100000+5;
const int MOD = 998244353;
const int INF = 1e9+7;

inline int read() {
    int x = 0,f=1;
    char ch = getchar();
    for(; ch<'0'||'9'<ch; ch=getchar()) if(ch == '-') f=-1;
    for(; '0'<=ch&&ch<='9'; ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}

#define lal puts("****");
#define pb push_back
#define mp make_pair

/********************************************************/

/*************SPLAY-tree************/

int n,m;

int ch[N][2];  //ch[][0] lson ch[][1] rson
int f[N];      //father
int sz[N];     //size
int val[N];    //value of node_i
int cnt[N];    // counts of the node_i
int root;      //root of splay-tree
int tot;       //tot,total,is the number of node of tree

void pushup(int x){
    if(x)sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+cnt[x];
}

void rotate(int x,int k){   // k = 0 左旋, k = 1 右旋
    int y=f[x];int z=f[y];
    ch[y][!k]=ch[x][k];if(ch[x][k])f[ch[x][k]]=y;
    f[x]=z;if(z)ch[z][ch[z][1]==y]=x;
    f[y]=x;ch[x][k]=y;
    pushup(y),pushup(x);
}

void splay(int x,int goal){
    for(int y=f[x];f[x]!=goal;y=f[x])
        rotate(x,(ch[y][0]==x));
    if(goal==0) root=x;
}

void newnode(int rt,int v,int fa){
//    printf("newnode : rt =  %d\n",rt);
    f[rt]=fa;val[rt]=v,sz[rt]=cnt[rt]=1;
    ch[rt][0]=ch[rt][1]=0;
}

void delnode(int &rt){ //其实是为内存回收做准备的  回头再完善
    f[rt]=val[rt]=sz[rt]=cnt[rt]=0;
    ch[rt][0]=ch[rt][1]=rt=0;
}

/***************************以下是DEBUG***************************/

void Traversal(int rt){
    if(!rt) return;
    Traversal(ch[rt][0]);
    printf("%d f[]=%d sz[]=%d lson=%d rson=%d val[]=%d\n",rt,f[rt],sz[rt],ch[rt][0],ch[rt][1],val[rt]);
    Traversal(ch[rt][1]);
}
void debug(){
    printf("ROOT = %d <---\n",root);
    Traversal(root);
}

/**************************以下是前置操作**************************/

//以x为根的子树 的极值点  0 极小 1 极大
int extreme(int x,int k){
    while(ch[x][k])x=ch[x][k];splay(x,0);
    return x;
}
//以x为根的子树 第k个数的位置
int kth(int x,int k){
    if(sz[ch[x][0]]+1<=k&&k<=sz[ch[x][0]]+cnt[x]) return x;
    else if(sz[ch[x][0]]>=k) return kth(ch[x][0],k);
    else return kth(ch[x][1],k-sz[ch[x][0]]-cnt[x]);
}
int search(int rt,int x){
        if(ch[rt][0]&&val[rt]>x) return search(ch[rt][0],x);
    else if(ch[rt][1]&&val[rt]<x)return search(ch[rt][1],x);
    else return rt;
}
/***************************以下是正经操作*************************/
//前驱
int prec(int x){
    int k=search(root,x);
    splay(k,0);//debug();
    if(val[k]<x) return k;
    return extreme(ch[k][0],1);
}
//后继
int sufc(int x){
    int k=search(root,x);
    splay(k,0);//debug();
    if(val[k]>x) return k;
    return extreme(ch[k][1],0);
}
int rk(int x){
    int k=search(root,x);
    splay(k,0);
    return sz[ch[root][0]]+1;
}

//在第k个数后插入值为x的节点
void _insert(int k,int x){
    int r=kth(root,k),rr=kth(root,k+1);
    splay(r,0),splay(rr,r);

    newnode(++tot,x,rr);ch[rr][0]=tot;
    for(r=rr;r;r=f[r])pushup(r);
    splay(rr,0);
}

/*****************************************************/

struct node {
    int h,k;
}a[N];

bool cmp(node a,node b){
    return a.h>b.h;
}
int ans[N];
void dfs(int rt){
    if(!rt) return ;
    dfs(ch[rt][0]);
    ans[++ans[0]]=val[rt];
    dfs(ch[rt][1]);
}
int main(){
    int _ = 1,kcase = 0;
    for(scanf("%d",&_);_--;){
        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);
        printf("Case #%d:",++kcase);

        bool flag = false;
        for(int i=1;i<=n;i++) if(a[i].k>=i) flag =true;

        if(flag ){
            puts(" impossible");
            continue;
        }

        tot=0,root=1;
        newnode(++tot,-INF,0),newnode(++tot,INF,root);
        ch[root][1]=tot;

        for(int i=1;i<=n;i++){
            a[i].k=min(a[i].k,i-1-a[i].k);
            _insert(a[i].k+1,a[i].h);
        }

        ans[0]=0;
        dfs(root);
        for(int i=2;i<ans[0];i++)  printf(" %d",ans[i]);
        puts("");
    }
    return 0;
}

思路2:


①我们将序列按照身高从小到大排序,那么当前假设到第i个人入列的时候,我们之前入队的所有人都比他矮,那么他在如队的时候,只要身前留下min(k,n-i-k)个位子即可。

②所以我们一开始的时候,将一颗树状数组都置1,表示所有位子都还没有被占用过,过来一个人的时候,二分树上剩余位子的第min(k+1,n-i-k+1)个位子即可,如果存在这个位子,那么将这个人放到这个位子上,否则结果就是impossible.

③过程用树状数组套二分去跑就行了。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
    int h,k;
}a[100005];
int tree[100005];//树
int ans[100005];
int n;
int cmp(node a,node b)
{
    return a.h<b.h;
}
int lowbit(int x)//lowbit
{
    return x&(-x);
}
int sum(int x)//求和求的是比当前数小的数字之和,至于这里如何实现,很简单:int sum=sum(a[i]);
{
    int sum=0;
    while(x>0)
    {
        sum+=tree[x];
        x-=lowbit(x);
    }
    return sum;
}
void add(int x,int c)//加数据。
{
    while(x<=n)
    {
        tree[x]+=c;
        x+=lowbit(x);
    }
}
int main()
{
    int t;
    int kase=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(tree,0,sizeof(tree));
        for(int i=1;i<=n;i++)scanf("%d%d",&a[i].h,&a[i].k),add(i,1);
        sort(a+1,a+1+n,cmp);
        int flag=1;
        for(int i=1;i<=n;i++)
        {
            int pos=min(a[i].k+1,n-(i+a[i].k)+1);
            if(pos<1)flag=0;
            if(flag==0)break;
            int l=1;
            int r=n;
            int output=-1;
            while(r-l>=0)
            {
                int mid=(l+r)/2;
                if(sum(mid)<pos)
                {
                    l=mid+1;
                }
                else
                {
                    output=mid;
                    r=mid-1;
                }
            }
            if(output==-1)flag=0;
            else ans[output]=a[i].h,add(output,-1);
        }
        printf("Case #%d: ",++kase);
        if(flag==0)printf("impossible\n");
        else
        {
            for(int i=1;i<=n;i++)
            {
                if(i>1)printf(" ");
                printf("%d",ans[i]);
            }
            printf("\n");
        }
    }
}










<think> 我们正在讨论的是C++语言中的特性。用户询问的是包含`#include<bits/stdc++.h>`头文件的编程语言是什么。 根据引用[1][2][3][4]的内容,这个头文件是C++特有的,通常被称为“万能头文件”或“综合头文件”。 因此,我们的回答应该明确指出这是C++语言中的用法。 同时,根据引用内容,这个头文件并非C++标准的一部分,但被许多编译器支持(尤其是GCC),常用于竞赛编程中。 注意:引用[3]提到了一些OJ平台的支持情况(如POJ、HDU不支持,但CF、Topcoder支持),但我们无需在回答中详细展开,除非用户特别询问。 回答结构: 1. 直接给出答案:这是C++语言。 2. 简要解释这个头文件的作用(一次性包含多个标准库头文件)。 3. 说明其常见使用场景(竞赛编程)和注意事项(编译时间、可移植性等)。 4. 生成相关问题。 注意:我们不需要重复用户问题中已经包含的引用内容,而是提炼引用中的关键信息来支持我们的答案。 </think> 包含 `#include<bits/stdc++.h>` 综合头文件的编程语言是 **C++**。这是 C++ 语言特有的预编译指令,主要出现在 GNU GCC 编译器环境中。以下是关键说明: ### 核心特性 1. **非标准但广泛支持**: - 属于 GNU GCC 编译器的扩展特性[^1][^3] - 不被 C++ 标准委员会正式认可,但在竞赛编程中广泛应用 2. **"万能头文件"作用**: ```cpp #include <bits/stdc++.h> // 替代多个标准头文件 ``` - 自动包含几乎所有 C++ 标准库头文件(如 `<iostream>`, `<vector>`, `<algorithm>` 等)[^4] - 等价于手动包含数十个独立头文件 3. **典型应用场景**: | 场景 | 适用性 | 原因 | |------|--------|------| | 编程竞赛(Codeforces, LeetCode 等) | ✅ 推荐使用 | 缩短编码时间,避免遗漏头文件[^2] | | 企业级软件开发 | ❌ 不推荐 | 会增加编译时间和最终二进制体积[^3] | | 特定 OJ 平台(POJ, HDU) | ❌ 不支持 | 平台未实现此扩展[^3] | ### 使用示例 ```cpp #include <bits/stdc++.h> // 单行包含所有库 using namespace std; int main() { vector<int> data = {3,1,4,2}; sort(data.begin(), data.end()); // 直接使用算法库 cout << "排序结果: "; for(auto x : data) cout << x << " "; // 输出: 1 2 3 4 return 0; } ``` ### 注意事项 1. **可移植性问题** 部分编译器(如 MSVC)不支持此头文件,需改用标准头文件[^3]: ```cpp #include <iostream> #include <vector> #include <algorithm> // 标准替代方案 ``` 2. **编译效率** 包含未使用的库会导致: - 编译时间增加(约 20-30%) - 最终可执行文件体积膨胀[^3] 3. **学习建议** 初学者应优先学习标准头文件,避免产生依赖。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值