HDU 5493 Queue 树状数组
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维护一下就好了
@)1%KBO0HM418$J94$1R.jpg)
代码:
//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 树状数组的更多相关文章
- hdu 5493 Queue 树状数组第K大或者二分
Queue Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU 5493 Queue 树状数组+二分
Queue Problem Description N people numbered from 1 to N are waiting in a bank for service. They all ...
- hdu 5493 (树状数组)
题意:在一个队列中,你知道一个人在他左边或者右边比他高的人的个数,求字典序最小的答案 思路:先将人按 矮-->高 排序,然后算出在每个人前面需要预留的位置.树状数组(也可以线段树)解决时,先二 ...
- hdu 5497 Inversion 树状数组 逆序对,单点修改
Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...
- hdu 4000Fruit Ninja 树状数组
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- HDU 2838 (DP+树状数组维护带权排序)
Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...
- HDU 2689Sort it 树状数组 逆序对
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu 4046 Panda 树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...
- HDU 3584 三维树状数组
三维树状数组模版.优化不动了. #include <set> #include <map> #include <stack> #include <cmath& ...
随机推荐
- Eclipse中将classes文件删除之后显示:找不到或无法加载主类解决方案
第一步: 将Eclipse自动编译打开 Project -> Build Automatically 第二步: Eclipse - Project - Clean
- node安装插件方法
node安装插件方法有几种,这里列出常用的两种方法: 方法1: 进入要安装插件的目录,直接用 npm 软件安装包安装,如(安装express): cd /project npm install -g ...
- win设置壁纸
默认壁纸图片位置: C:\Windows\Web\Wallpaper\Scenes 你可以自己建文件夹,放自己喜欢的桌面壁纸. 设置壁纸: 桌面右键 -> 个性化 然后点击 “桌面背景” - ...
- java之内部类与匿名内部类
Java 内部类 分四种:成员内部类.局部内部类.静态内部类和匿名内部类. 1.成员内部类: 即作为外部类的一个成员存在,与外部类的属性.方法并列. 注意:成员内部类中不能定义静态变量,但可以访问外部 ...
- poj 水题系列
题目:http://poj.org/problem?id=3006 筛选法求素数 #include <iostream> #include<cstdio> #include&l ...
- 斜率优化dp(POJ1180 Uva1451)
学这个斜率优化dp却找到这个真心容易出错的题目,其中要从n倒过来到1的确实没有想到,另外斜率优化dp的算法一开始看网上各种大牛博客自以为懂了,最后才发现是错了. 不过觉得看那些博客中都是用文字来描述, ...
- GRIB格式转换心得(转自博客:http://windforestwing.blog.163.com/blog/static/19545412007103084743804/)
1.wgrib的使用 在cmd命令行下键入wgrib后即可察看wgrib相关命令参数,简要介绍如下: l Inventory/diagnostic–output selections 详 ...
- 【转】Cocos2d-x 弹出对话框的设计与实现
转自:http://www.tairan.com/archives/4854 我们时常需要这么些功能,弹出一个层,给与用户一些提示,这也是一种模态窗口,在没有对当前对话框进行确认的时候,不能继续往下操 ...
- HDU 2602 Find a way BFS搜索
题意:找到总时间最少的KFC 分析:两遍BFS 找KFC比较一下 注:有些地方的KFC可能到达不了,wa了一次 #include <iostream> #include <cstdi ...
- C++基类和派生类之间的转换
本文讲解内容的前提是派生类继承基类的方式是公有继承,关键字public 以下程序为讲解用例. #include<iostream> using namespace std; class A ...