POJ2828(插队问题)
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 17077 | Accepted: 8466 |
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 Valiin 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.
线段树处理。注意 0<=pos[i]<=i-1
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
struct Acmer{
int pos,val;
}acmer[MAXN];
struct node{
int l,r;
int sum;
}a[MAXN*]; int que[MAXN];
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
if(l==r)
{
a[rt].sum=;
return;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
a[rt].sum=a[rt<<].sum+a[(rt<<)|].sum;
}
void update(int rt,int pos,int val)
{
if(a[rt].r==a[rt].l)
{
que[a[rt].l]=val;
a[rt].sum--;
return ;
}
//pos-1 比其先来插队且位置在其前方的人数
if(pos<=a[rt<<].sum) update(rt<<,pos,val);//后来的排在前面(左面)
else update((rt<<)|,pos-a[rt<<].sum,val);//没有位置,留下空位置给排在前面的,自己向后排
a[rt].sum=a[rt<<].sum+a[(rt<<)|].sum;
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)
{
scanf("%d%d",&acmer[i].pos,&acmer[i].val);
}
build(,,n);
for(int i=n-;i>=;i--)//最后一个位置是确定的,前一个根据后一个位置调整。
{
update(,acmer[i].pos+,acmer[i].val);
}
for(int i=;i<=n-;i++)
{
printf("%d ",que[i]);
}
printf("%d\n",que[n]);
}
return ;
}
POJ2828(插队问题)的更多相关文章
- 【poj2828】Buy Tickets 线段树 插队问题
[poj2828]Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in ...
- POJ2828 Buy Tickets(线段树之插队问题)
飞翔 问题是这样的:现在有n个人要买票,但是天黑可以随便插队.依次给出将要买票的n个人的数据信息.包含两项:pos,当前第i号人来了之后他肯定要插入到pos这个位置,如果当前pos无人,那最好了,直接 ...
- 【插队问题-线段树-思维巧妙】【poj2828】Buy Tickets
可耻的看了题解 巧妙的思维 逆序插入,pos 代表的意义为前面要有pos个空格才OK: 证明:仔细思考一下就觉得是正确的,但是要想到这种方式还是要很聪明,空格是前面的几个数字所形成的,所以要特地留出来 ...
- poj2828 Buy Tickets (线段树 插队问题)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 22097 Accepted: 10834 Des ...
- POJ-2828 Buy Tickets---线段树+逆序
题目链接: https://cn.vjudge.net/problem/POJ-2828 题目大意: 插队的问题,每个案例给出n,代表有n个插队的,每个给出p,v,意思是代号为v的人插在了第p个人的后 ...
- [POJ2828] Buy Tickets(待续)
[POJ2828] Buy Tickets(待续) 题目大意:多组测试,每组给出\(n\)条信息\((a,b)\),表示\(b\)前面有\(a\)个人,顺序靠后的信息优先级高 Solution.1 由 ...
- POJ2828 Buy Tickets[树状数组第k小值 倒序]
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19012 Accepted: 9442 Desc ...
- 【POJ2828】Buy Tickets(线段树)
题意:有一个输入序列,每次操作要把b[i]插入到第a[i]个,在第a[i]个后面的要后移,问最后序列. n<=200000 思路:顺序来只能用splay维护 考虑倒序,对于插入到第K个位置,在线 ...
- pojBuy Tickets2828线段树或者树状数组(队列中倒序插队)
这题开始的思路就是模拟:就像数组中插点一样,每一个想买票的人都想往前插队! 但是这样的话肯定TLE, 看了别人的思路之后才恍然大悟! 正解: 将开始的正序插入,变成倒序插入,这样的话,想一想:第 i ...
随机推荐
- oracle查询数据库资源位置
archival log list; 归档日志文件位置 select file_name from dba_data_files; 查询数据库文件位置 select parameter control ...
- JQuery利用选择器定位动态id?
假如我们需要去定位一个动态生成的div,我们需要为它指定一个动态的id 例如: 前台使用EL进行迭代LIST生成div,为其添加动态的id,生成之后变成下面样式 <div id="tz ...
- Java多线程面试问题
这篇文章主要是对多线程的面试问题进行总结的,罗列了40个多线程的问题. 1. 多线程有什么用? 一个可能在很多人看来很扯淡的一个问题:我会用多线程就好了,还管它有什么用?在我看来,这个回答更扯淡.所谓 ...
- 认识XmlReader
认识XmlReader 摘要 XmlReader类是组成.NET的关键技术之一,极大地方便了开发人员对Xml的操作.通过本文您将对XmlReader有一个很好的认识,并将其应用到实际开发中. 目录 ...
- 关于-O0、O1、O2、O3优化
少优化->多优化: O0 -->> O1 -->> O2 -->> O3 -O0表示没有优化,-O1为缺省值,-O3优化级别最高 整理自网络,仅供参考 1.- ...
- 设计TCP服务器的规则
设计TCP服务器,采用如下规则: 1.正等待连接请求的一端有一个固定长度的连接队列,该队列中的连接已被TCP接受(完成三次握手),但还没有被应用层接受.注意:TCP接受一个连接是将其放入这个队列,而应 ...
- js闭包实际用途
闭包例:防止双击 在线商店的购物车里,为防止“多重购买”,需要防止按钮被双击. 下面用“jQuery + 闭包”来实现这一功能. HTML <form name="frm" ...
- 九度OJ 1178:复数集合 (插入排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8393 解决:1551 题目描述: 一个复数(x+iy)集合,两种操作作用在该集合上: 1.Pop 表示读出集合中复数模值最大的那个复数,如 ...
- 使用ab 进行并发压力测试
使用ab 进行并发压力测试 - 参与商 - 博客园 https://www.cnblogs.com/shenshangzz/p/8340640.html 使用ab 进行并发压力测试 ab全称为:a ...
- python数据分析之:数据清理,转换,合并,重塑(二)
一:移除重复数据 DataFrame经常出现重复行,就像下面的这样 In [7]: data=DataFrame({'k1':['one']*3+['two']*4,'k2':[1,1,2,3,3,4 ...