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 ...
随机推荐
- JSONObject遍历并替换部分json值
今天做接口对接,在更新价格时,最开始传的值为整数,发现报错,询问对方后得知需要统一保留两位小数,没有则为.00,于是对原有JSONObject进行改造,遍历并替换其中的值.下面贴出代码: JSONOb ...
- 3 (mysql实战) 事务隔离
提到事务,你肯定不陌生,和数据库打交道的时候,我们总是会用到事务.最经典的例子就是转账,你要给朋友小王转 100 块钱,而此时你的银行卡只有 100 块钱. 转账过程具体到程序里会有一系列的操作,比如 ...
- IDEA中找不到spring的配置文件,或者不存在某个目录(比如没有src 目录)
比如 项目中src目录找不到了,解决方式为: 这类问题都是设置这儿.
- Codeforces1304F.Animal Observation
分析一下得知是DP问题,时间复杂度符合,设dp[i][j]为从第i天开始,第j个位置能得到的最大值,其有三种转移状态 1.与上一天的选择有重合 2.与上一天的选择没有重合,且上一天的选择在左边 3.与 ...
- C语言中的指针与数组的定义与使用
指针的特点 他就是内存中的一个地址 指针本身运算 指针所指向的内容是可以操作的 操作系统是如何管理内存的 栈空间 4M~8m的大小 当进入函数的时候会进行压栈数据 堆空间 4g的大小 1g是操作系统 ...
- 「快学springboot」16.让swagger帮忙写接口文档
swagger简介 官方的介绍 THE WORLD'S MOST POPULAR API TOOLING Swagger is the world's largest framework of API ...
- 【快学springboot】11.整合redis实现session共享
前言 这里都是基于前面的项目基础上的.springboot整合redis非常的方便,这也是springboot的宗旨,简化配置.这篇文章就教大家如何使用springboot整合redis来实现sess ...
- monkey常见API及实例
一.API简介 LaunchActivity(pkg_name, cl_name):启动应用的Activity.参数:包名和启动的Activity. Tap(x, y, tapDuration): 模 ...
- 第1节 kafka消息队列:1、kafka基本介绍以及与传统消息队列的对比
1. Kafka介绍 l Apache Kafka是一个开源消息系统,由Scala写成.是由Apache软件基金会开发的一个开源消息系统项目. l Kafka最初是由LinkedIn开发,并于20 ...
- 二十三、java连接oracle数据库操作:jdbc
1.jdbc 1) 含义:JDBC是java语言连接数据库,Java Date Base Connectivity2) jdbc的本质:在编程时java程序会去连接不同的数据库,而每个数据库的底层的实 ...