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 ...
随机推荐
- MathType编辑表示右上角的符号的方法
MathType是一款公式编辑器,其中的符号种类非常的全,如果需要编辑一些专业的公式,用它非常的方便快捷.但是有些符号由于不常用,在一些模板之中使用的时候需要深入地去查找调用一下.比如表示右上角的符号 ...
- OBS (open boardcast server)结构分析
一,简介 OBS(open boardcast server),是一个用于直播的开源软件. 官方网站:https://obsproject.com/ 代码托管地址:https://github.com ...
- day5笔记
一.上节回顾: 1,find通过元素找索引,可切片,找不到返回-12,index,找不到报错.3,split 由字符串分割成列表,默认按空格.4,captalize 首字母大写,其他字母小写.5,up ...
- Python 使用MySQL
在导入MySQLdb之前,需要安装MySQLdb模块.使用pip安装,命令如下: pip install MySQL-python 安装成功后,导入MySQLdb模块 import MySQLdb 连 ...
- GIT快速学习
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001373962845513ae ...
- 【BZOJ3620】似乎在梦中见过的样子 KMP
[BZOJ3620]似乎在梦中见过的样子 Description “Madoka,不要相信 QB!”伴随着 Homura 的失望地喊叫,Madoka 与 QB 签订了契约. 这是 Modoka 的一个 ...
- android菜鸟学习笔记31----Android使用百度地图API(二)获取地理位置及地图控制器的简单使用
1.获取当前地理位置: Android中提供了一个LocationManager的类,用于管理地理位置.不能通过构造函数获取该类的实例,而是通过Context的getSystemService(): ...
- 2014-08-28——移动端web开发,基本Meta标签
1.<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scal ...
- Java的默认构造函数调用
// 注意,这里不能是 public class OOO,否则编译无法通过,需把文件命名成 OOO.java class OOO { // 注意:如果不定义OOO(),那么Shapes(int i)编 ...
- Python中的默认参数(转)
add by zhj: Python设计者为何将默认参数设计成这样呢?参见Python函数参数默认值的陷阱和原理深究 原文:https://github.com/acmerfight/insight_ ...