Queue

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

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

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
10 0
20 0
30 1

Sample Output

Case #1: 20 10 30
Case #2: 10 20 30
Case #3: impossible

HINT

题意

给你一些人,每个人的身高都是不一样的

然后再给你一个k,表示这个人的左边或者右边,有k个人比他高

然后让你构造一个序列,满足这个条件

题解:

每个人其实可以站两个位置,不是左边就是右边

你从小到大插入的话,那么剩下没插的位置都是比你大的

你就插进去就好

用树状数组/splay/treap维护一下就好了

代码:

//qscqesze
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200006
#define mod 1000000007
#define eps 1e-9
#define e exp(1.0)
#define PI acos(-1)
#define lowbit(x) (x)&(-x)
const double EP = 1E- ;
int Num;
//const int inf=0x7fffffff;
const ll inf=;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//*************************************************************************************
int n,val1[maxn],val2[maxn]; void add(int * vc,int u ,int v)
{
while(u <= n )
{
vc[u] += v;
u += lowbit(u);
}
} int query(int * vc,int u)
{
int res=;
while(u)
{
res += vc[u];
u -= lowbit(u);
}
return res;
} int RankGet(int * vc,int k)
{
int L = , R = n;
while(L < R)
{
int mid = L + ((R-L)>>);
int res = query(vc,mid);
//cout << "mid is " << mid << " res is " << res << endl;
if(res == k) R = mid;
else if(res < k) L = mid + ;
else R = mid - ;
}
return L;
} struct kkk
{
int x,y;
};
bool cmp(kkk aa,kkk bb)
{
return aa.x<bb.x;
}
kkk p[maxn];
int ans[maxn];
vector<int> v1,v2;
int main()
{
// freopen("in.txt","r",stdin);
int t=read();
for(int cas=;cas<=t;cas++)
{
n=read();
for(int i=;i<=n;i++)
{
p[i].x=read(),p[i].y=read();
p[i].y++;
}
sort(p+,p+n+,cmp);
int flag=;
memset(val1,,*(n+));memset(val2,,*(n+));
for(int i = ; i <= n ; ++ i)
{
add(val1,i,);
add(val2,i,);
}
//return 0;
for(int i=;i<=n;i++)
{
if(p[i].y>n-i+)
{
flag=;
break;
}
int p1 = RankGet(val1,p[i].y);
int p2 = n-RankGet(val2,p[i].y) + ;
// cout << "i is " << i << " p1 is " << p1 << " p2 is " << p2 << endl;
// int p1=v1[p[i].y],p2=v2[p[i].y];
if(p1<p2)
{
ans[p1]=p[i].x;
add(val1,p1,-);
add(val2,n-p1+,-);
//cout << "up delete " << p1 << " down delete " << n-p1 +1 << endl;
//v1.erase(v1.begin()+p[i].y);
//v2.erase(v2.begin()+n-i+2-p[i].y);
}
else
{
ans[p2]=p[i].x;
add(val1,p2,-);
add(val2,n-p2+,-);
// cout << "up delete " << p2 << " down delete " << n-p2 +1 << endl;
//v2.erase(v2.begin()+p[i].y);
//v1.erase(v1.begin()+n-i+2-p[i].y);
}
// cout << endl;
}
printf("Case #%d:",cas);
if(!flag)
{
printf(" impossible\n");
}
else
{
for(int i=;i<=n;i++)
printf(" %d",ans[i]);
printf("\n");
}
}
}

HDU 5493 Queue 树状数组的更多相关文章

  1. hdu 5493 Queue 树状数组第K大或者二分

    Queue Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  2. HDU 5493 Queue 树状数组+二分

    Queue Problem Description N people numbered from 1 to N are waiting in a bank for service. They all ...

  3. hdu 5493 (树状数组)

    题意:在一个队列中,你知道一个人在他左边或者右边比他高的人的个数,求字典序最小的答案 思路:先将人按  矮-->高 排序,然后算出在每个人前面需要预留的位置.树状数组(也可以线段树)解决时,先二 ...

  4. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

  5. hdu 4000Fruit Ninja 树状数组

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  6. HDU 2838 (DP+树状数组维护带权排序)

    Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...

  7. HDU 2689Sort it 树状数组 逆序对

    Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  8. hdu 4046 Panda 树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...

  9. HDU 3584 三维树状数组

    三维树状数组模版.优化不动了. #include <set> #include <map> #include <stack> #include <cmath& ...

随机推荐

  1. Counting sheep...

    Counting sheep... Description: Consider an array of sheep where some sheep may be missing from their ...

  2. Android开发之bug-No Activity found to handle Intent

    android.content.ActivityNotFoundException: No Activity found to handle Intent 做Android开发中,使用隐式intent ...

  3. Codeforces Round #221 (Div. 2) Lever I.O.U.

    这场cf 做的很差,,第一题犯了一个很低级的错误..把i写成了J.... 第二题 想的太复杂了...其实我们只需要 考虑每个人自己的负债情况就行了,就是假设每个人把别人 欠他的钱,拿过来还给别人..这 ...

  4. vfp 操作excel

    VFP全面控制EXCEL 收藏 VFP和Excel都可以用来进行处理数据库表格,如果巧妙地将二者的优点结合起来,将会大大方便我们的工作.比如我们可以利用VFP进行处理数据,而利用Excel的预览打印功 ...

  5. CSS基础深入之细说盒子模型

    Html任何一个元素(element)都可以当成一个盒子(box)来看待,可以结合现实中的盒子来理解下文,下文其中一些单词应该是通俗易懂的需要记录的单词. 基本情况 每一个盒子都有一个内容区域(con ...

  6. Gentoo安装

    Gentoo Linux安装详解--根据官方WiKi整理 时间:2014-06-26 06:37:54      阅读:549      评论:0      收藏:0      [点我收藏+] 标签: ...

  7. SGU 438 The Glorious Karlutka River =) ★(动态+分层网络流)

    [题意]有一条东西向流淌的河,宽为W,河中有N块石头,每块石头的坐标(Xi, Yi)和最大承受人数Ci已知.现在有M个游客在河的南岸,他们想穿越这条河流,但是每个人每次最远只能跳D米,每跳一次耗时1秒 ...

  8. OK335xS EMMC Partition hacking

    #! /bin/sh # # OK335xS EMMC Partition hacking # 说明: # 本文主要是为了解读同事对EMMC分区的写法,其中有很多写法重复了,但 # 依然尽量保留其作者 ...

  9. 【转】java枚举类型enum的使用

    原文网址:http://blog.csdn.net/wgw335363240/article/details/6359614 java 枚举类型enum 的使用 最近跟同事讨论问题的时候,突然同事提到 ...

  10. 学习面试题Day08

    1.TCP/IP协议的理解.     TCP/IP定义了电子设备(如计算机)连入因特网的标准,以及数据如何在它们之间传输的标准.它既是互联网中的基本通信语言或协议,也是局域网的通信协议.     TC ...