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 ...
随机推荐
- uboot生成随机的MAC地址
转载:http://blog.chinaunix.net/uid-25885064-id-3303132.html 在使用U-boot时,有个问题就是MAC地址的设置,如果MAC地址相同的两块开发板在 ...
- Mysql 5.7.18 加密连接mysql_ssl_rsa_setup
MySQL 5.7.18 下载地址: https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64. ...
- 目标跟踪之粒子滤波---Opencv实现粒子滤波算法
目标跟踪学习笔记_2(particle filter初探1) 目标跟踪学习笔记_3(particle filter初探2) 前面2篇博客已经提到当粒子数增加时会内存报错,后面又仔细查了下程序,是代码方 ...
- 机器学习三 -- 用Python实现K-近邻算法
Python语言实现机器学习的K-近邻算法 写在前面 额...最近开始学习机器学习嘛,网上找到一本关于机器学习的书籍,名字叫做<机器学习实战>.很巧的是,这本书里的算法是用Python语言 ...
- shader学习之一:Properties语义块支持的数据类型
_Int ("Int",Int)=2为:变量名("面板显示的名称",数据类型) 对于Int,Float,Range这些数字类型的属性,默认值为单独的数字.对于贴 ...
- jqPaginator-master | kkpager-master 这两个分页插件的使用方法
首先:百度"分页插件" 就会 找到这条链接: url=X8P3UpOM-6ceSfjdngX0oh9cNmVwSDy94CxKqWIazhyZ7If4S8wgpPqyEGUhk2t ...
- 【BZOJ4942】[Noi2017]整数 线段树+DFS(卡过)
[BZOJ4942][Noi2017]整数 题目描述去uoj 题解:如果只有加法,那么直接暴力即可...(因为1的数量最多nlogn个) 先考虑加法,比较显然的做法就是将A二进制分解成log位,然后依 ...
- ajax学习笔记(3)--$.get()方法
<head runat="server"> <title>jQuery中的$.get()方法</title> <script src=&q ...
- 九度OJ 1003:A+B
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:15078 解决:6299 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开. 现在请计 ...
- TCP/UDP server
Simple: Sample TCP/UDP server https://msdn.microsoft.com/en-us/library/aa231754(v=vs.60).aspx Simple ...