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 ...
随机推荐
- TensorFlow基础二(Shape)
首先说明tf中tensor有两种shape,分别为static (inferred) shape和dynamic (true) shape,其中static shape用于构建图,由创建这个tenso ...
- [原]JointJS流程图
最近项目上需要用流程图来做问题定界分析,之前有同事用jsPlumb做过,但是阅读代码后觉得比较麻烦,所以自己又找了一圈,找到一个叫Dagre-D3的开源类库,画出来的效果如下图,Dagre-D3最大的 ...
- spring-cloud-服务调用流程
- 华为学习配置笔记-01 配置con密码
1.配置con密码, <Huawei>system //进入系统视图 [Huawei]user-interface console 0 //进入con接口 [Huawei ...
- h5 穿透滚动
引子 h5 页面有弹窗浮层时,浮层之下若产生了滚动,滑动浮层时会让其产生滚动.这是示例页面,移动端访问如下: Origin My GitHub 原因 找到的信息里面有两种说法: 使用了 -webkit ...
- 「BJOI2018」求和
「BJOI2018」求和 传送门 观察到 \(k\) 很小而且模数不会变,所以我们直接预处理 \(k\) 取所有值时树上前缀答案,查询的时候差分一下即可. 参考代码: #include <alg ...
- K8S LoadBalance 私有环境解决方案 == metallb 工具安装和使用介绍
接着上文,排除故障后,我重新配置了metallb组件到k8s环境. metallb为k8s service 的loadbalance负载方式提供免费的解决方案. external-ip的收费方案,可以 ...
- 警示框UIAlertController的使用(看完马上会用!!)
本文尽量图文并茂,并且提供对应的代码,确保看到这篇文章马上能够上手使用UIAlertController控件.-我要兑现我的务实宣言- 本文构思: 1.出具效果图,通过这种最直接方式了解该控件的展示效 ...
- 【转载】C#常用控件属性及方法介绍
C#常用控件属性及方法介绍 目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文 ...
- ubuntu16.04 使用tensorflow object detection训练自己的模型
一.构建自己的数据集 1.格式必须为jpg.jpeg或png. 2.在models/research/object_detection文件夹下创建images文件夹,在images文件夹下创建trai ...