Queue

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

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
10 0
20 0
30 1
 
Sample Output
Case #1: 20 10 30
Case #2: 10 20 30
Case #3: impossible
 
Source
题意:一个队列,把队列拆散,每个人知道自己前面或者后面比他高的人的数量;
   求原先的队列;
思路:刚刚开始看并没有什么思路,接着模拟一下就会发现思路,虽然复杂度高,接着只需要想如何优化; 
   因为要求字典序最小, 我们可以先按照身高从小到大排序,假设当前到了第i高的人, 他前面或者
   后面有k个人, 那么他前面的所有人都比他矮, 比他高的还有n-i个人,那么假设他前面还有p个空
   位, 他就是第p+1个空位上的人, 那么怎么求p呢?  因为要求字典序最小, 所以 p = min(k, n - i - k)。
   为什么这样是对的呢?每个人有两个可能位置啊,  因为他之前的都比他矮,  所以他无论在哪个位置都是可以的。
   那么为了让字典序最小, 就选择一个较小的位置。   当n - i - k < 0 时, 说明没有多余空格, 那么无解。
  (取别人的博客http://doc.okbase.net/weizhuwyzc000/archive/197172.html)
  一些小错误,运算符优先级(*/)>(+-)>(>>,<<) 所以记得上括号(wa N遍的感受)

#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大或者二分的更多相关文章

  1. HDU 5493 Queue 树状数组

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

  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. hrbust 1840 (树状数组第k大) 删点使用

    小橙子 Time Limit: 2000 MS Memory Limit: 32768 K Total Submit: 2(2 users) Total Accepted: 1(1 users) Ra ...

  5. HDU 5249 离线树状数组求第k大+离散化

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

  6. POJ2828 Buy Tickets[树状数组第k小值 倒序]

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19012   Accepted: 9442 Desc ...

  7. hdu 4000Fruit Ninja 树状数组

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

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

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

  9. hdu 4046 Panda 树状数组

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

随机推荐

  1. python——asyncio模块实现协程、异步编程

    我们都知道,现在的服务器开发对于IO调度的优先级控制权已经不再依靠系统,都希望采用协程的方式实现高效的并发任务,如js.lua等在异步协程方面都做的很强大. Python在3.4版本也加入了协程的概念 ...

  2. __import__ 与动态加载 python module

    原文出处: koala bear    Direct use of __import__() is rare, except in cases where you want to import a m ...

  3. 计划评审技术PERT

    概念 编辑 PERT(Program Evaluation and Review Technique)即 [2]  计划评审技术,最早是由美国海军在计划和控制北极星导弹的研制时发展起来的.PERT技术 ...

  4. 机器学习理论基础学习18---高斯过程回归(GPR)

    一.高斯(分布)过程(随机过程)是什么? 一维高斯分布 多维高斯分布 无限维高斯分布   高斯网络 高斯过程 简单的说,就是一系列关于连续域(时间或空间)的随机变量的联合,而且针对每一个时间或是空间点 ...

  5. 2.keras实现-->深度学习用于文本和序列

    1.将文本数据预处理为有用的数据表示 将文本分割成单词(token),并将每一个单词转换为一个向量 将文本分割成单字符(token),并将每一个字符转换为一个向量 提取单词或字符的n-gram(tok ...

  6. Locust性能测试4-参数关联

    前言 前面[Locust性能测试2-先登录场景案例]讲了登录的案例,这种是直接传账号和密码就能登录了,有些登录的网站会复杂一点, 需要先从页面上动态获取参数,作为登录接口的请求参数,如[学信网:htt ...

  7. Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  8. SQL查询遍历数据方法一 [ 临时表 + While循环]

    以下以SQL Server 2000中的NorthWind数据库中的Customers表为例, 用 临时表 + While循环 的方法, 对Customers表中的CompanyName列进行遍历 c ...

  9. sql 中延时操作

    select 1; WAITFOR DELAY '00:00:30'; select 2; --执行完第一个之后会 延时 30秒,才会执行第二个sql

  10. transition过度效果 + transform旋转360度

    css样式: .animate{ width:65px; height:40px; background:#92B901; color:#ffffff; position:absolute; font ...