hdu 5439 Queue 树状数组+二分

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5493

Queue

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


 

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 (T≤1000).
Each test case starts with a line containing an integer N indicating the number of people in the queue (1≤N≤100000). Each of the next N lines consists of two integers hi and ki as described above (1≤hi≤109,0≤ki≤N−1). 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 1

0 0

20 0

30 1

Sample Output

Case #1: 20 10 30

Case #2: 10 20 30

Case #3: impossible

题意:

给定人的身高,和排在他前面或后面比他高的人的个数。构造一个序列使得满足,并且字典序最小。

解题思路:

二分+树状数组

可以先按照身高从小到大排序,假设当前到了第i高的人,他前面或者后面有k个人,那么他前面的所有人都比他矮,比他高的还有n-i个人,因为要求字典序最小,所以他在未排的人中的位置 p = min(k, n - i - k)(寻找前面放高的还是后面放高的字典序小)。每个人有两个可能位置,因为他之前的都比他矮,那么为了让字典序最小,就选择一个较小的位置。

当n - i - k < 0 时,说明没有多余空格,那么无解。

二分第i个人的位置, 用树状数组判断他前面有多少人以此确定空余的位置数。 

This is the code

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int INF = 0x3f3f3f3f;

struct node
{
    int h, k;
} a[100009];
int c[100009];
int ans[100009];
int n;

int lowbit(int x)
{
    return x&-x;
}

void add(int x,int val)
{
    while(x<=n)
    {
        c[x]+=val;
        x+=lowbit(x);
    }
}

int getSum(int x)
{
    int sum=0;
    while(x)
    {
        sum+=c[x];
        x-=lowbit(x);
    }
    return sum;
}

bool cmp(node a, node b)
{
    return a.h < b.h;
}

int Find(int tem)//寻找最靠前的位置
{
    int l=1;
    int r=n;
    int mid;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(getSum(mid)>=tem)
            r=mid-1;
        else
            l=mid+1;
    }
    return r+1;
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;++t)
    {
        memset(c,0,sizeof c);
        scanf("%d", &n);
        for (int i=1; i<=n; i++)
        {
            scanf("%d%d",&a[i].h,&a[i].k);
            add(i,1);//1表示空位,0表示已经有人了
        }
        sort(a+1,a+1+n,cmp);//排序将身高矮的排在钱买你
        bool flag=true;
        for(int i=1; i<=n; i++)//逐个枚举每个人的位置
        {
            int k=n-i;//表示还有k个人比i高
            if(a[i].k > k)//表示不能满足有a[i].k个人在i的前面或者后面
            {
                flag=false;
                break;
            }
            int temp=min(a[i].k,k-a[i].k)+1;//+1表示本身的这个位置
            //寻找往前面放高的人位置靠前,还是往后面放高的位置靠前
            int pos=Find(temp);
            ans[pos]=a[i].h;
            add(pos, -1);//将这个位置占领
        }
        if (!flag)
            printf("Case #%d: impossible\n",t);
        else
        {
            printf("Case #%d:", t);
            for (int i=1; i<=n; i++)
                printf(" %d",ans[i]);
            printf("\n");
        }
    }
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值