POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 19725 | Accepted: 9756 |
Description
Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…
The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.
It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!
People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped
the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.
Input
There will be several test cases in the input. Each test case consists of
N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next
N lines contain the pairs of values Posi and Vali in the increasing order of
i (1 ≤ i ≤ N). For each i, the ranges and meanings of
Posi and Vali are as follows:
- Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the
Posi-th person in the queue. The booking office was considered the 0th person and the person at the front of the queue was considered the first person in the queue. - Vali ∈ [0, 32767] — The i-th person was assigned the value
Vali.
There no blank lines between test cases. Proceed to the end of input.
Output
For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.
Sample Input
4
0 77
1 51
1 33
2 69
4
0 20523
1 19243
1 3890
0 31492
Sample Output
77 33 69 51
31492 20523 3890 19243
Hint
The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.
思路:线段树 倒序插入 更新节点时,左右子树要区分,如果左子树的空位大于等于现在要插入的位置,说明前面的空位没有占去。否则,进入右子树,而进入右子树时,空位变成了pos减去所有左子树的空位。即便左子树有空位但也不能插入,因为该数还没有插入,必须预留空位。
代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn=200005;
int sum[maxn<<2],res[maxn<<2];
int data[maxn],location[maxn];
bool flag=false;
void pushup(int rt) {
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l, int r, int rt) {
sum[rt]=r-l+1;
if(l==r) return;
int mid=(l+r)>>1;
build(lson);
build(rson);
}
void update(int pos, int val, int l, int r, int rt) {
if(l==r) {
res[rt]=val;sum[rt]--;return;
}
int mid=(l+r)>>1;
if(pos<=sum[rt<<1]) update(pos,val,lson);
else update(pos-sum[rt<<1],val,rson);//要预留空位
pushup(rt);
}
void show(int l, int r, int rt) {
if(l==r) {
if(flag==false) printf("%d",res[rt]),flag=true;
else printf(" %d",res[rt]);
return;
}
int mid=(l+r)>>1;
show(lson);
show(rson);
}
int main() {
int n;
while(~scanf("%d",&n)) {
for(int i=0;i<n;++i) {
scanf("%d%d",&location[i],&data[i]);
}
build(1,n,1);
for(int i=n-1;i>=0;--i) {
update(location[i]+1,data[i],1,n,1);
}
flag=false;
show(1,n,1);
printf("\n");
}
return 0;
}
POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)的更多相关文章
- poj 2828 Buy Tickets (线段树(排队插入后输出序列))
http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissio ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- POJ 2828 Buy Tickets | 线段树的喵用
题意: 给你n次插队操作,每次两个数,pos,w,意为在pos后插入一个权值为w的数; 最后输出1~n的权值 题解: 首先可以发现,最后一次插入的位置是准确的位置 所以这个就变成了若干个子问题, 所以 ...
- POJ 2828 Buy Tickets(线段树·插队)
题意 n个人排队 每一个人都有个属性值 依次输入n个pos[i] val[i] 表示第i个人直接插到当前第pos[i]个人后面 他的属性值为val[i] 要求最后依次输出队中各个人的属性 ...
- POJ 2828 Buy Tickets(线段树单点)
https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的 ...
- poj 2828 Buy Tickets (线段树)
题目:http://poj.org/problem?id=2828 题意:有n个人插队,给定插队的先后顺序和插在哪个位置还有每个人的val,求插队结束后队伍各位置的val. 线段树里比较简单的题目了, ...
- POJ - 2828 Buy Tickets (段树单点更新)
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
- POJ 2828 Buy Tickets(单点更新) 详细题解和思路
题意:给n个人插队,输出最后的队伍情况(题意写的有些粗糙) 思路:第一点:在最后的队伍中,我们唯一能确定的是最后一个人一定能排到指定位置.那么,倒数第二个是在最后一个基础上确定位置的,这样一层一层的倒 ...
- POJ 2828 Buy Tickets(排队问题,线段树应用)
POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意: 排队买票时候插队. 给出一些数对,分别代表某个人的想要插入的位 ...
随机推荐
- 想成为一个高效的Web开发者吗?来看看大牛分享的经验吧~ #精选JAVASCRIPT前端开发
想成为一个高效的Web开发者吗?来看看大牛分享的经验吧~ 作为一个软(ku)件(bi)工(de)程(ma)师(nong),你有没有觉得做什么事都没时间?没时间学习新东西,没时间去回顾.整理原来写的烂代 ...
- SE6 不定参数和默认参数详解和使用细节
在SE5以前我们通常通过arguments类数组对象来引用不定形参,SE6则使用了一种叫做不定参数的写法,比起隐式的arguments要直观的多. 不定参数使用...参数名来指定一个不定参数,参数名指 ...
- 【每天半小时学框架】——React.js的模板语法与组件概念
[重点提前说:组件化与虚拟DOM是React.js的核心理念!] 先抛出一个论题:在React.js中,JSX语法提倡将 HTML 和 CSS 全都写入到JavaScrip ...
- scroll 区域滚动
网页内都有快速滚动和回弹的效果: overflow: scroll; -webkit-overflow-scrolling: touch; 实际上,Safari用了原生控件来实现,对于有-webk ...
- linux 安装 Elasticsearch5.6.x 详细步骤以及问题解决方案
在网上有很多那种ES步骤和问题的解决 方案的,不过没有一个详细的整合,和问题的梳理:我就想着闲暇之余,来记录一下自己安装的过程以及碰到的问题和心得:有什么不对的和问题希望及时拍砖. 第一步:环境 li ...
- Oracle 11g服务器安装详细步骤——图文教程
1.大家可以根据自己的操作系统是多少位(32位或64位)的,到官网下载相应的安装程序,如下图所示. 有一点需要注意,Oracle的安装程序分成2个文件,下载后将2个文件解压到同一目录即可. 2.下载完 ...
- javascript 二维(多维)数组的复制问题
最近在项目中遇到一个动画暂停的效果,需要在动画停止的时候检测当前坐标和已经运行的时间,从而调节时间轴为再次运行时加速. 但是在数组保存方面折腾了半天. var orbitArray = [], lin ...
- Linux系列教程(十六)——Linux权限管理之ACL权限
通过前面的两篇博客我们介绍了Linux系统的用户管理,Linux用户和用户组管理之相关配置文件 讲解了用户管理的相关配置文件,包括用户信息文件/etc/passwd,用户密码文件/etc/shadow ...
- 修改 Sublime 按快捷键 ctrl+s 自动格式化(reindent lines)的问题
Sublime 工具自带代码格式化的功能,但在某些场景下格式化代码后并不是我们想要的代码格式,且是点击保存ctrl+s才触发的格式代码事件,so,为关闭点击ctrl+s格式代码,我们需要改命令 sav ...
- VIM于换行EOL的思考
\n LF 0A 将当前光标切换到下一行(不一定行首)\r CR OD 将当前光标置于行首 在windows与unix系统中,unix将\n代表换行并置于行首,而windows保持原意.即unix:\ ...