poj 2828 Buy Tickets 【买票插队找位置 输出最后的位置序列+线段树】
题目地址:http://poj.org/problem?id=2828
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个人要买票,但是天黑可以随便插队。依次给出将要买票的n个人的数据信息。包含两项:pos,当前第i号人来了之后他肯定要
插入到pos这个位置,如果当前pos无人,那最好了,直接把他插入即可。但如果pos这个位置有人了,从现实意义上讲,第i号人插入之后,相当于他

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <iostream>
#include <algorithm> using namespace std; struct seq
{
int pos, val;
}a[200000+10]; int num[800000+100];
int ans[200000+100]; void Build(int rt, int ll, int rr)
{
num[rt]=rr-ll+1;
if(ll==rr)
return;//已经到达根节点
Build(rt*2, ll, (ll+rr)/2 );
Build(rt*2+1, (ll+rr)/2+1, rr );
} int update(int pos, int rt, int ll, int rr)
{
num[rt]--;//空位置数量-1
if(ll==rr)
return ll;//到达根节点
if(num[rt*2]>pos)//pos是从0开始的 而线段树是从1开始存储的 但0和1在此题中是对应存储的
return update(pos, rt*2, ll, (ll+rr)/2);
else
return update(pos-num[rt*2], rt*2+1, (ll+rr)/2+1, rr);
} int main()
{
int n, i;
while(scanf("%d", &n)!=EOF)
{
for(i=0; i<n; i++){
scanf("%d %d", &a[i].pos, &a[i].val);
}//打表保存
Build(1, 1, n);//从1号节点开始建树 区间[1,n] /* for(i=1; i<=7; i++)
printf("%d---", num[i]); */ for(i=n-1; i>=0; i--){
ans[update(a[i].pos, 1, 1, n)]=a[i].val;
//
}
for(i=1; i<=n; i++){
printf("%d%c", ans[i], i==n?'\n':' ');
}
}
return 0;
}
poj 2828 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 题意 排队买票,依次给出当前人要插队的位置,每个人有个编号,然后问你最后整个的序列是什么? 分析 最后一个人的要插入的位置是确定的,所以逆 ...
- 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 Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 16273 Accepted: 8098 Desc ...
- 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 Description Railway tickets were d ...
随机推荐
- Android桌面小组件的使用
一:建立一个类继承AppWidgetProvider 二:建立AWP的布局文件: 布局自己定义一个,但是在使用控件上是有要求的: 以上是Widget目前支持的控件. 三:编写AWP的信息文件:需要在r ...
- MySQL右连接
1.语法:select 字段列表 from table1 别名1 right join table2 别名2 on 连接条件 [where 子句]
- hibernate 懒加载图解
- [转]JavaWeb之 Servlet执行过程 与 生命周期
https://www.cnblogs.com/vmax-tam/p/4122105.html Servlet的概念 什么是Servlet呢? Java中有一个叫Servlet的接口,如果一个普通的类 ...
- PowerDesigner之设置(2)——扩展属性
数据库:SQL2000 PD版本:16 在PowerDesigner之设置(1)中,创建表的表头注释部分由于中文的原因无法定长,这里我们用PD的扩展方法来解决. 具体方法如下: 主菜单DataBase ...
- 《从零开始学Swift》学习笔记(Day2)——使用Web网站编写Swift代码
Swift 2.0学习笔记——使用Web网站编写Swift代码 原创文章,欢迎转载.转载请注明:关东升的博客 Swift程序不能在Windows其他平台编译和运行,有人提供了一个网站swiftstub ...
- Springboot中读取自定义名称properties的
Springboot读取自定义的配置文件时候,使用@value,一定要指定配置文件的位置! 否则报错参数化异常!
- 简单的 ajax demo
2.最重要也是最核心的是要自己改下bootstrap-paginator.js源文件,如下: [javascript] view plain copy function oneferRepo ...
- spring web中完成单元测试
对于在springweb总完单元测试,之前找过些资料,摸索了很久,记录下最终自己使用的方法 1,创建测试类,创建测试资源文件夹 src/test/resources/WEB_INFO/conf 将工程 ...
- C 和 C++ 的标准库分别有自己的 locale 操作方法,C 标准库的 locale 设定函数是 setlocale(),而 C++ 标准库有 locale 类和流对象的 imbue() 方法(gcc使用zh_CN.GBK,或者zh_CN.UTF-8,VC++使用Chinese_People's Republic of China.936或者65001.)
转自:http://zyxhome.org/wp/cc-prog-lang/c-stdlib-setlocale-usage-note/ [在此向原文作者说声谢谢!若有读者看到文章转载时请写该转载地址 ...