HDU-- Buy Tickets
告知每次要插到第 i 个位置上,问最后它们的顺序是什么。
这一题,不是考线段树,是考如何想出用线段树...思维很巧妙,倒过来做的话就能确定此人所在的位置....
线段树每个结点有一个remain域,记录些线段还有多少个空位....开始时,叶结点的remain当然为1
Buy Tickets
Time Limit : 8000/4000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 10 Accepted Submission(s) : 5
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.
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.
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.
#include<stdio.h>
#define maxn 500004
int a[maxn];
int b[maxn];
int c[maxn];
struct node
{
int left,right;
int remain;
}; node tree[*maxn];
void build(int left,int right,int i)
{
tree[i].left =left;
tree[i].right =right;
if(tree[i].left ==tree[i].right )
{
tree[i].remain=;
return ;
}
build(left,(left+right)/,*i);
build((left+right)/+,right,*i+);
tree[i].remain=tree[*i].remain+tree[*i+].remain;
}
void insert(int p,int val,int k)
{
if(tree[k].left == tree[k].right)
{
tree[k].remain--;
a[tree[k].left] = val;
return ;
}
if(p <= tree[k<<].remain)
insert(p,val,*k);
else
insert(p-tree[*k].remain,val,*k+); //右子树 tree[k].remain = tree[*k].remain + tree[*k+].remain; //更新remain
} int main()
{
int i,n;
while(~scanf("%d",&n))
{
for(i=;i<=n;i++)
scanf("%d%d",&b[i],&c[i]);
build(,n-,);
for(i=n;i>=;i--)
{
insert(b[i]+,c[i],);
}
printf("%d",a[]);
for(i=;i<n;i++)
printf(" %d",a[i]);
printf("\n");
}
return ;
}
HDU-- Buy Tickets的更多相关文章
- hdu 2828 Buy Tickets
Buy Tickets Time Limit : 8000/4000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- poj 2828 buy Tickets 用线段树模拟带插入的队列
Buy Tickets Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2 ...
- HDU 1260 Tickets (普通dp)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1260 Tickets Time Limit: 2000/1000 MS (Java/Others) ...
- 题解报告:hdu 1260 Tickets
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260 Problem Description Jesus, what a great movie! T ...
- H - Buy Tickets POJ - 2828 逆序遍历 树状数组+二分
H - Buy Tickets POJ - 2828 这个题目还是比较简单的,其实有思路,不过中途又断了,最后写了一发别的想法的T了. 然后脑子就有点糊涂,不应该啊,这个题目应该会写才对,这个和之前的 ...
- POJ2828 Buy Tickets[树状数组第k小值 倒序]
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19012 Accepted: 9442 Desc ...
- POJ 2828 Buy Tickets(线段树 树状数组/单点更新)
题目链接: 传送门 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Description Railway tickets were d ...
- Buy Tickets(线段树)
Buy Tickets Time Limit:4000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- 【poj2828】Buy Tickets 线段树 插队问题
[poj2828]Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in ...
- Buy Tickets
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 16010 Accepted: 7983 Descript ...
随机推荐
- jjQuery 源码分析1: 整体结构
目前阅读的是jQuery 1.11.3的源码,有参考nuysoft的资料. 原来比较喜欢在自己的Evernote上做学习基类,并没有在网上写技术博客的习惯,现在开始学习JS的开源代码,想跟大家多交流, ...
- java ,js获取web工程路径
一.java获取web工程路径 1),在servlet可以用一下方法取得: request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/” ...
- HDU 2501 Tiling_easy version(简单递推)
Tiling_easy version Problem Description 有一个大小是 2 x n 的网格,现在需要用2种规格的骨牌铺满,骨牌规格分别是 2 x 1 和 2 x 2,请计算一共有 ...
- kindeditor编辑器代码过滤解决方法.
很多朋友在使用Kindeditor编辑器的时候都会遇到这样一个问题,如:给A标签加上title属性过后,浏览的时候,却神奇般地发现title属性没有了.再次切换html源代码的时候,返现编辑器将tit ...
- python 三分钟入门
1.Python环境配置(2.7版本): Python官网:https://www.python.org/ Pycharm官网 http://www.jetbrains.com/pycharm/dow ...
- OrderedDict
OrderedDict 使用dict时,Key是无序的.在对dict做迭代时,我们无法确定Key的顺序. 如果要保持Key的顺序,可以用OrderedDict: >>> from c ...
- vs2012生成的项目,如何在只装有VS2010的电脑上打开
步骤: 1.用记事本打开Vs2012生成的项目解决方案文件(.sln文件)文件 2.修改前两行 Microsoft Visual Studio Solution File, Format Versio ...
- poj 1144 Network
Network 题意:输入n(n < 100)个点,不一定是连通图,问有多少个割点? 割点:删除某个点之后,图的联通分量增加. 思路:dfs利用时间戳dfs_clock的特性,点u的low函数l ...
- su: Bad item passed to pam_*_item()
su: Bad item passed to pam_*_item() 查看 /etc/default/locale 该文件应该只含义如下格式的文字: LANG=en_US.UTF-8 如何还没有解决 ...
- DES原理与实现
一 DES综述 DES是对称密码的一种,它使用56位秘钥对64位长分组进行加密.DES对每个分组的内容都会进行16轮迭代,每轮的操作相同但是对应不同的子秘钥.所有的子秘钥都是由主密钥推导而来. 64位 ...