博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5493 Queue(线段树)
阅读量:4139 次
发布时间:2019-05-25

本文共 3484 字,大约阅读时间需要 11 分钟。

Queue

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 736 Accepted Submission(s): 396
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
h_i. The
i-th person remembers that there were
k_i 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
k_i in a wrong direction.
k_i 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
h_i and
k_i as described above (
1≤h_i≤10^9,0≤k_i≤N-1). Note that the order of the given
h_i and
k_i is randomly shuffled.
The sum of
N over all test cases will not exceed
10^6
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
3310 120 130 0310 020 130 0310 020 030 1
Sample Output
Case #1: 20 10 30Case #2: 10 20 30Case #3: impossible
Source
#include 
#include
#include
#include
using namespace std;const int N=1e5+10;struct Node{ int h,x;}node[N];int re[N],sum[N*4];bool cmp(Node a,Node b){ return a.h
>1; int ne=now<<1; Build(ne,le,mid); Build(ne+1,mid+1,ri); sum[now]=sum[ne+1]+sum[ne]; //这段区间有多少个点没被用 }void Update(int now,int le,int ri,int pos,int val){ if(le==ri){ sum[now]=0; //该点被使用 re[le]=val; //插入位置 /*for(int i=1;i<=3;i++) printf(" %d",re[i]); printf("\n");*/ return; } int mid=(le+ri)>>1; int ne=now<<1; if(pos<=sum[ne]) //这个区间段未使用的点够,则进入 Update(ne,le,mid,pos,val); else //不够,则进入右子树,要减去前面左子树未使用的点,因为左子树在前面 Update(ne+1,mid+1,ri,pos-sum[ne],val); sum[now]=sum[ne+1]+sum[ne];}int main(){ int t,i,n,j; scanf("%d",&t); for(j=1;j<=t;j++){ bool ok=1; // memset(sum,0,sizeof(sum)); // memset(re,0,sizeof(re)); scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d%d",&node[i].h,&node[i].x); Build(1,1,n); sort(node+1,node+n+1,cmp); for(i=1;i<=n;i++){ int m=n-i; //还有多少个比现在的高 因为前面的人矮 不能算 要n-i int temp=m-node[i].x; if(temp<0) { ok=0; break; } //temp指后面方向高Node[i].x人的话的位置在哪 // Node[i].x指前面方向的话位置在哪; //然后2个选靠前的 即更小的 if(node[i].x<=temp) //在前面预留node[i].x人 插入到 node[i].x+1位置 //如果存在就一直往后推,这个在Update里实现 Update(1,1,n,node[i].x+1,node[i].h); else Update(1,1,n,temp+1,node[i].h); } printf("Case #%d:",j); if(ok){ for(i=1;i<=n;i++) printf(" %d",re[i]); printf("\n"); } else printf(" impossible\n"); } return 0;}/*9981 02 33 14 25 36 27 18 0*/

转载地址:http://ifmvi.baihongyu.com/

你可能感兴趣的文章
为什么说程序员是“培训班出来的”就是鄙视呢?
查看>>
码农吐糟同事:写代码低调点不行么?空格回车键与你有仇吗?
查看>>
阿里p8程序员四年提交6000次代码的确有功,但一次错误让人唏嘘!
查看>>
一道技术问题引起的遐想,最后得出结论技术的本质是多么的朴实!
查看>>
985硕士:非科班自学编程感觉还不如培训班出来的,硕士白读了?
查看>>
你准备写代码到多少岁?程序员们是这么回答的!
查看>>
码农:和产品对一天需求,产品经理的需求是对完了,可我代码呢?
查看>>
程序员过年回家该怎么给亲戚朋友解释自己的职业?
查看>>
技术架构师的日常工作是什么?网友:搭框架,写公共方法?
查看>>
第四章 微信飞机大战
查看>>
九度:题目1008:最短路径问题
查看>>
九度Online Judge
查看>>
九度:题目1027:欧拉回路
查看>>
九度:题目1012:畅通工程
查看>>
九度:题目1017:还是畅通工程
查看>>
九度:题目1034:寻找大富翁
查看>>
第六章 背包问题——01背包
查看>>
第七章 背包问题——完全背包
查看>>
51nod 分类
查看>>
1136 . 欧拉函数
查看>>