题目链接:

Queue

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

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

题意:

给出n个人的高度,以及这个人前面或者后面有多少个比她高,现在让你求字典序最小的队列的身高;

思路:

排序后可以得到这个队列中比某个人高的总人数假设是num,如果num<k比这个总人数还多的话就不可能了;

然后我们把这个k变成前边的比她高的人数,这个k是min(k,num-k)这样保证了字典序最小;

然后就是求每个位置上的身高呢,将身高从小大排序,这就是相当于给了一个序列的逆序数,然后让你还原这个序列了;

从小到大贪心,对于每个人二分她的位置,确定后更新到树状数组中,复杂度O(n*log2n);

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+10;
int n,ans[maxn],sum[maxn],hi[maxn];
struct node
{int h,k,id;
}po[maxn];
int cmp(node a,node b){return a.h>b.h;}
int cmp1(node a,node b)
{if(a.h==b.h)return a.k<b.k;return a.h<b.h;
}
inline int lowbit(int x){return x&(-x);}
inline void update(int x)
{while(x<=n){sum[x]++;x+=lowbit(x);}
}
inline int query(int x)
{int s=0;while(x){s+=sum[x];x-=lowbit(x);}return s;
}
int main()
{  int t,Case=0;scanf("%d",&t);while(t--){printf("Case #%d:",++Case);scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d%d",&po[i].h,&po[i].k),po[i].id=i;sort(po+1,po+n+1,cmp);int flag=0,num=0;po[0].h=-1;for(int i=1;i<=n;i++){if(po[i].h!=po[i-1].h)num=i-1;if(po[i].k>num){flag=1;break;}hi[i]=num;}if(flag)printf(" impossible\n");else {for(int i=1;i<=n;i++)po[i].k=min(po[i].k,hi[i]-po[i].k),sum[i]=0;sort(po+1,po+n+1,cmp1);for(int i=1;i<=n;i++){int l=po[i].k+1,r=n;while(l<=r){int mid=(l+r)>>1;if(mid-query(mid)>po[i].k)r=mid-1;else l=mid+1;}ans[r+1]=po[i].h;update(r+1);}for(int i=1;i<=n;i++)printf(" %d",ans[i]);printf("\n");}}return 0;
}

  

转载于:https://www.cnblogs.com/zhangchengc919/p/5951261.html

hdu-5493 Queue(二分+树状数组)相关推荐

  1. 【bzoj4009】[HNOI2015]接水果 DFS序+树上倍增+整体二分+树状数组

    题目描述 给出一棵n个点的树,给定m条路径,每条路径有一个权值.q次询问求一个路径包含的所有给定路径中权值第k小的. 输入 第一行三个数 n和P 和Q,表示树的大小和盘子的个数和水果的个数. 接下来n ...

  2. jzoj4050-寻宝游戏【二分,树状数组,LCA】

    正题 题目链接:https://jzoj.net/senior/#contest/show/3017/1 题目大意 nnn个点的一棵树,mmm次操作,修改一个地方的宝藏. 每次操作后求拿完所以宝藏并回 ...

  3. bzoj4418 [Shoi2013]扇形面积并 扫描线+二分+树状数组

    Description 给定N个同心的扇形,求有多少面积,被至少K个扇形所覆盖. 对于100%的数据,1≤n≤105, 1≤m≤106,1≤k≤5000,1≤ri≤105,-m≤a1,a2≤m Sol ...

  4. HDU多校1 - 6756 Finding a MEX(分块+二分+树状数组)

    题目链接:点击查看 题目大意:给出一个 n 个点和 m 条边的无向图,每个点都有一个权值,现在需要执行 q 次操作,每次操作分为两种类型: 1 pos val :将第 pos 个点的权值修改为 val ...

  5. hdu(4339)树状数组+二分查找

    /* s1[i]与s2[i]匹配,树状数组i位置更新1,否则更新0.*/#include<stdio.h> #include<string.h> #include<alg ...

  6. P3527 [POI2011]MET-Meteors 整体二分 + 树状数组

    洛谷 题意: 思路: 考虑整体二分前,一定要思考一下直接二分怎么做.显然对每个城市,当<pos<pos<pos的时候收集不够足够的陨石,>=pos>=pos>=po ...

  7. P3573-[POI2014]RAJ-Rally【拓扑排序,二分+树状数组】

    正题 题目链接:https://www.luogu.com.cn/problem/P3573 题目大意 nnn个点mmm条边的DAGDAGDAG,删掉一个点使得最长路最短. 解题思路 先跑一遍拓扑排序 ...

  8. ACM学习历程—51NOD 1685 第K大区间2(二分 树状数组 中位数)

    http://www.51nod.com/contest/problem.html#!problemId=1685 这是这次BSG白山极客挑战赛的E题. 这题可以二分答案t. 关键在于,对于一个t,如 ...

  9. 【CodeForces - 460C】Present(二分+树状数组)

    题干: 给定N朵花的原先的高度,从左到右排列,最多浇水m天,每天只能浇一次,每次使得连续的w朵花的高度增长1,问最后最矮的花的高度最高是多少. Examples Input 6 2 3 2 2 2 2 ...

最新文章

  1. Win关闭开启软件时的弹窗
  2. Using join buffer (Batched Key Access)
  3. HTML5 获得canvas油漆环境
  4. 点讯输入法S60数字键通用版V6.0(官方签名正式版)
  5. Design7:数据删除设计
  6. linux mysql跑高_linux 下如何查看mysql跑了哪些服务
  7. 盒子模型(W3C盒子和IE盒子)
  8. MIPI - DVP
  9. 软件工程实践专题第三次团队作业
  10. vector元素个数_STL之vector
  11. ubuntu20.04 安装、美化、办公环境搭建及深度学习开发环境搭建
  12. 2.1 InnoDB存储引擎(概述、版本、体系结构)
  13. UDP数据包的产生和发送
  14. 《跨界杂谈》华为印象(二):MTS
  15. 利用WireShark下载视频网站的流媒体视频
  16. 举头皮皮虾机器人_一种仿生水下皮皮虾机器人通信系统的制作方法
  17. codevs2069 油画 — 动态维护优先队列
  18. 小红书美妆报告:18岁以下群体对美妆消费量提升158%
  19. Vue2.0 内置指令directives 与全局配置过滤filters
  20. 探索四自由的机械臂动力学

热门文章

  1. LightOJ 1013 LCS+记忆化搜索
  2. 《构建之法》第八章自习感想与知识点
  3. 使用laypage进行分页
  4. 不定高宽的元素居中的方法
  5. 用rz,sz命令在xshell传输文件
  6. 响应在此上下文中不可用
  7. 18号是什么php,19年1月18号CSS浮动float
  8. (85)Vivado 多周期路径约束情况
  9. (25)二分频verilog与VHDL编码(学无止境)
  10. 1 D触发器verilog与Systemverilog编码