hdu 5493 Queue 树状数组第K大或者二分
Queue
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 862 Accepted Submission(s): 449
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?
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
3
10 1
20 1
30 0
3
10 0
20 1
30 0
3
10 0
20 0
30 1
Case #2: 10 20 30
Case #3: impossible
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define mod 1000000007
int scan()
{
int res = 0 , ch ;
while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
{
if( ch == EOF ) return 1 << 30 ;
}
res = ch - '0' ;
while( ( ch = getchar() ) >= '0' && ch <= '9' )
res = res * 10 + ( ch - '0' ) ;
return res ;
}
#define maxn (1<<18)
struct is
{
int h,k;
}a[maxn];
int cmp(is x,is y)
{
return x.h<y.h;
}
int tree[maxn],n;
int q[maxn];//原队列;
int lowbit(int x)
{
return x&-x;
}
void update(int x,int change)
{
while(x<=n)
{
tree[x]+=change;
x+=lowbit(x);
}
}
int k_thfind(int K)//树状数组求第K小
{
int sum=0;
for(int i=18;i>=0;i--)
{
if(sum+(1<<i)<=n&&tree[sum+(1<<i)]<K)
{
K-=tree[sum+(1<<i)];
sum+=1<<i;
}
}
return sum+1;
}
int main()
{
int x,y,z,i,t;
int gg=1;
scanf("%d",&x);
while(x--)
{
memset(tree,0,sizeof(tree));
int flag=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d%d",&a[i].h,&a[i].k);
sort(a+1,a+n+1,cmp);
printf("Case #%d:",gg++);
for(i=1;i<=n;i++)
update(i,1);
for(i=1;i<=n;i++)
{
if(n-i<a[i].k) flag=1;
if(flag)break;
int pos1=k_thfind(a[i].k+1);
int pos2=k_thfind(n-a[i].k+1-i);
//cout<<pos1<<"\t"<<pos2<<endl;
if(pos1<pos2)
{
q[pos1]=a[i].h;
update(pos1,-1);
}
else
{
q[pos2]=a[i].h;
update(pos2,-1);
}
}
if(flag)
printf(" impossible\n");
else
{
for(i=1;i<=n;i++)
printf(" %d",q[i]);
printf("\n");
}
}
return 0;
}
hdu 5493 Queue 树状数组第K大或者二分的更多相关文章
- HDU 5493 Queue 树状数组
Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...
- HDU 5493 Queue 树状数组+二分
Queue Problem Description N people numbered from 1 to N are waiting in a bank for service. They all ...
- hdu 5493 (树状数组)
题意:在一个队列中,你知道一个人在他左边或者右边比他高的人的个数,求字典序最小的答案 思路:先将人按 矮-->高 排序,然后算出在每个人前面需要预留的位置.树状数组(也可以线段树)解决时,先二 ...
- hrbust 1840 (树状数组第k大) 删点使用
小橙子 Time Limit: 2000 MS Memory Limit: 32768 K Total Submit: 2(2 users) Total Accepted: 1(1 users) Ra ...
- HDU 5249 离线树状数组求第k大+离散化
KPI Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- POJ2828 Buy Tickets[树状数组第k小值 倒序]
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19012 Accepted: 9442 Desc ...
- hdu 4000Fruit Ninja 树状数组
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- 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 ...
随机推荐
- gradle多工程依赖
多工程依赖:http://blog.csdn.net/w8452960/article/details/53415415 https://www.cnblogs.com/softidea/p/4525 ...
- HTML输入框的默认显示内容
在某些情况下我们会需要在输入框里默认显示一些内容,比如在登录的时候不在输入框前面显示用户名和密码,直接在输入框里显示,这时只要在input的标签里添加属性 placeholder="用户名 ...
- Java-单向链表算法
/** * 数据结构之链表(单向链表) * @author Administrator * */ public class LinkNodeTest { public static void main ...
- iOS 界面启动时,功能新特征显示
APP新启动的时候,都会有几张新的图片滑动,才能到主的界面.现在,我们新建一个控制器,专门来处理新特性,直接上代码. 第一步:新建一个NewfeatureController // // HWNewf ...
- 树莓派3Braspberry pi 如何汉化显示中文教程
树莓派默认是采用英文字库的,而且系统里没有预装中文字库,所以即使你在locale中改成中文,也不会显示中文,只会显示一堆方块.因此需要我们手动来安装中文字体. 好在有一个中文字体是免费开源使用的.ss ...
- http协议基础(十)实体首部字段
1.定义 包含在请求和响应中的实体部分所使用的首部,用于补充内容的更新时间等与实体相关的信息 2.Allow 通知客户端能够支持的Request-URI指定资源的所有http方法:如果服务器接收到不支 ...
- suiyi
<?php namespace app\controllers; use Yii;use app\models\Device;use app\models\DeviceSearch;use ap ...
- Codeforces Round #247 (Div. 2) C D
这题是一个背包问题 这样的 在一个k子树上 每个节点都有自己的k个孩子 然后 从原点走 走到 某个点的 和为 N 且每条的 长度不小于D 就暂停问这样的 路有多少条, 呵呵 想到了 这样做没有把他敲 ...
- Intro to Python for Data Science Learning 5 - Packages
Packages From:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-3-functio ...
- Linux中Postfix反病毒和垃圾邮件工具(十)
amavisd-new amavisd-new呼叫器是一个连接MTA和内容检测工具(诸如病毒扫描工具和SpamAssassin)的高性能接口程序,使用perl语言写成.它一般通过SMTP.ESMTP或 ...