POJ 2829 Buy Tickets
Buy Tickets
| Time Limit: 4000MS | Memory Limit: 65536K | |
| Total Submissions: 26443 | Accepted: 12639 |
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.

题意:买票的时候有n个人插队,输入每个人插队的位置pos和插队的时间val,如果两个人同一个位置,时间小的在前面,按从前到后输出队伍最后的排列顺序
题解:先初始化线段树,第i个位置前面有i个位置,从最后一个数据开始更新线段树,每处理完一个数据,空位置数减一,处理到最后即可
输入样例eg:
空 空 空 空
空 空 空 69
空 空 33 69
空 空 33 51 69//在33和69之间插入,没有占用位置
空 77 33 51 69
注:用cin的话,就算用加速一样会超时
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
using namespace std;
int n;
int tree[], pos[], val[], ans[];
void build(int num, int le, int ri)
{
tree[num] = ri - le + ;//初始化空格数量
if (le == ri)
{
return;
}
int mid = (le + ri) / ;//mid设为全局变量答案就错了
build(num * , le, mid);
build(num * + , mid + , ri);
} void update(int num, int le, int ri, int x, int y)
{
tree[num]--;//占用之后空格数减一;
if (le == ri)
{
ans[le] = y;
return;
}
int mid = (le + ri) / ;
if (x <= tree[num * ])
update(num * , le, mid, x, y);
else
update(num * + , mid + , ri, x - tree[num * ], y);
}
int main()
{
// ios::sync_with_stdio(false);
while (~scanf("%d",&n))
{
for (int i = ; i <= n; i++)
{
scanf("%d%d",&pos[i],&val[i]);
pos[i] = pos[i] + ;//初始化,i位置(包括i)前有几个空位置
}
build(, , n);
for (int i = n; i >= ; i--)//从最后一个人开始插队
{
update(, , n, pos[i], val[i]);
}
for (int i = ; i <= n; i++)
{
if (i == )
printf("%d",ans[i]);
else
printf(" %d",ans[i]);
}
printf("\n");
}
return ;
}
POJ 2829 Buy Tickets的更多相关文章
- POJ 2828 Buy Tickets(排队问题,线段树应用)
POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意: 排队买票时候插队. 给出一些数对,分别代表某个人的想要插入的位 ...
- poj 2828 Buy Tickets(树状数组 | 线段树)
题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...
- poj 2828 Buy Tickets 【线段树点更新】
题目:id=2828" target="_blank">poj 2828 Buy Tickets 题意:有n个人排队,每一个人有一个价值和要插的位置,然后当要插的位 ...
- 线段树(单点更新) POJ 2828 Buy tickets
题目传送门 /* 结点存储下面有几个空位 每次从根结点往下找找到该插入的位置, 同时更新每个节点的值 */ #include <cstdio> #define lson l, m, rt ...
- poj 2828 Buy Tickets (线段树(排队插入后输出序列))
http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissio ...
- poj 2828 Buy Tickets 树状数组
Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in China, so ...
- poj 2828 Buy Tickets (线段树 单节点 查询位置更新)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 15533 Accepted: 7759 Desc ...
- POJ 2828 Buy Tickets(线段树 树状数组/单点更新)
题目链接: 传送门 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Description Railway tickets were d ...
- poj 2828 Buy Tickets【线段树单点更新】【逆序输入】
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 16273 Accepted: 8098 Desc ...
随机推荐
- 3_06_MSSQL课程_Ado.Net_接口、委托、事件、观察者模式
1.接口——实现接口 2.委托.事件(定义事件.注册事件.触发事件) 3.接口和事件的区别,怎么分情况用? 4.观察者模式作为设计模式的一种,也称发布订阅模式. 应对类型的变化和个数的变化. 中介设计 ...
- 支持USB4的Linux 5.6,有望在今年4月份推出
导读 根据外媒Phoronix的报道,Linux 5.6将支持USB4,. USB4的规范在去年9月份发布,基于雷电3,并与之向后兼容.英特尔的开源部门在去年10月份添加了USB4的初始补丁. 据报道 ...
- Linux centosVMware Linux监控平台介绍、zabbix监控介绍、安装zabbix、忘记Admin密码如何做
一.Linux监控平台介绍 cacti.nagios.zabbix.smokeping.open-falcon等等 cacti.smokeping偏向于基础监控,成图非常漂亮 cacti.nagios ...
- [经验] SpringBoot 远程连接 Linux 上的 Redis
开发环境: ---------- springboot 2.X ---------- Linux Ubuntu 18.0.04 关于怎么在 Ubuntu 上安装 Linux , 网上的教程一大堆, 这 ...
- idea右键新建选项没有类和包的创建方式
Intelidea创建好项目之后,右键新建Java class的时候发现没有改选项,只有以下几个选项 把sec目录设为源码目录,首先打开Project Structure
- 从零开始编写IntelliJ IDEA插件
写Java代码的时候,经常会涉及到重复性的操作,这个时候就会想要是有这样一个插件就好了,如果是大家都会遇到的场景,IDE或许已经提供了,再不然也有可能有人编写了相关的插件.要是这个操作是你们的编码环境 ...
- js中每隔一段时间执行一次
window.setInterval("flushs()",1000);
- Sqlserver2012 使用sql语句增加(或删除)表一个字段
前言 Mark在SqlServer 2012 的数据库使用sql语句增加(或删除)一张表的一个字段. 使用Sql语句增加表的一个字段 [1]语法: alter table table_name add ...
- postgres登录失败Connection refused与SSL off失败
连接失败问题 使用postgres数据库连接工具测试,遇到两次失败 第一个登录失败问题 Connection to 192.168.XX.XX:5432 refused. Check that the ...
- Mysql数据库日志,备份及回滚操作
一.打开二进制日志配置 : 在Windows系统下,对mysql的my.ini的log-bin等进行配置目录位置时,假设要将log-bin的日志配置到D盘的mysqllog的文件为binlog.则可以 ...