POJ P2828 Buy Ticket——线段树的其他信息维护
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.
--by POJ
http://poj.org/problem?id=2828
题目大意:
操作 i val;
序列在i+1位置插入点val;
输出最终的序列结果;
多组数据;
相似的题目 POJ hotel
(学习不深入的结果就是导致对一个点重复学习)
对一个序列预留空位,维护空位个数;
由于后操作影响前操作,故倒序维护操作;
对于每个操作 i val ,把她的val放在当前第i+1个空位上,然后占用该空位;
总结:
线段树可以维护对序列的加点操作,方法如上;
可以维护对序列的合法区间覆盖操作方法见POJ hotel;
这两个操作有相似性——线段树查询合法位置:
对原序列合法的要求,
如,空位个数,显然满足区间叠加性质;
如,区间最大连续空子段,显然也满足;
关键是找出什么是合法二字的要求;
代码如下:
#include<cstdio>
using namespace std;
const int MAXN=;
int n;
int tree[MAXN<<];
int que[MAXN];
int pos[MAXN],val[MAXN];
void up(int );
void build(int ,int, int );
int find(int ,int ,int ,int );
int main()
{
int i,j,k;
while(scanf("%d",&n)==){
build(,n,);
for(i=;i<=n;i++)
scanf("%d%d",&pos[i],&val[i]);
for(i=n;i>=;i--){
pos[i]++;
j=find(,n,,pos[i]);
que[j]=val[i];
}
for(i=;i<=n;i++)
printf("%d ",que[i]);
printf("\n");
}
return ;
}
void up(int nu){
tree[nu]=tree[nu<<]+tree[nu<<|];
}
void build(int l,int r,int nu){
if(l==r){
tree[nu]=;
return ;
}
int mid=(l+r)>>;
build(l,mid,nu<<);
build(mid+,r,nu<<|);
up(nu);
}
int find(int l,int r,int nu,int x){
int mid=(l+r)>>,ans;
if(l==r){
tree[nu]=;
return l;
}
if(tree[nu<<]>=x)
ans=find(l,mid,nu<<,x);
else
ans=find(mid+,r,nu<<|,x-tree[nu<<]);
up(nu);
return ans;
}
POJ P2828 Buy Ticket——线段树的其他信息维护的更多相关文章
- poj 2828 Buy Tickets (线段树(排队插入后输出序列))
http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissio ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19725 Accepted: 9756 Desc ...
- POJ 2828 Buy Tickets | 线段树的喵用
题意: 给你n次插队操作,每次两个数,pos,w,意为在pos后插入一个权值为w的数; 最后输出1~n的权值 题解: 首先可以发现,最后一次插入的位置是准确的位置 所以这个就变成了若干个子问题, 所以 ...
- POJ 2828 Buy Tickets(线段树·插队)
题意 n个人排队 每一个人都有个属性值 依次输入n个pos[i] val[i] 表示第i个人直接插到当前第pos[i]个人后面 他的属性值为val[i] 要求最后依次输出队中各个人的属性 ...
- POJ 2828 Buy Tickets(线段树单点)
https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的 ...
- poj 2828 Buy Tickets (线段树)
题目:http://poj.org/problem?id=2828 题意:有n个人插队,给定插队的先后顺序和插在哪个位置还有每个人的val,求插队结束后队伍各位置的val. 线段树里比较简单的题目了, ...
- Buy Tickets 【POJ - 2828】【线段树】
题目链接 有N次操作,每次都是将第i个数放置在第pos个数的后面,并且这个数的值是val. 这个线段树的思维确实很好,我们可以发现,后面放进去的数,一定是强制位置的,而前面放的数,会随着后面的数进入而 ...
- [poj2828] Buy Tickets (线段树)
线段树 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must ...
随机推荐
- Keil RTX使用 os_mut_init 报Hard Fault 错误解决记录
首先确定你的软件是在互斥信号初始化的位置,在以下几个位置,将会报Hard Fault 错误: (1).os_sys_init_user 用户线程创建之前 (2).os_tsk_create_user之 ...
- vue重构后台管理系统调研
Q4要来了,我来这家公司已经一个季度了,通过对公司前端框架的整体认识,对业务的一些认识,发现,这些东西也都是可以重构,无论是v2,还是v3的代码. 首先就要那后台管理来开刀来,现有的技术框架就是php ...
- STM32-增量式旋转编码器测量
Development kit:MDK5.14 IDE:UV4 MCU:STM32F103C8T6 一.增量式旋转编码器 1.简介 编码器(encoder)是将信号(如比特流)或数据进行编制.转换为可 ...
- Bootstrap-datepicker日期时间选择器的简单使用
日期时间选择器 目前,bootstrap有两种日历.datepicker和datetimepicker,后者是前者的拓展. Bootstrap日期和时间组件: 使用示例: 从左到右依次是十年视图.年视 ...
- 洛谷 P2015 二叉苹果树 (树上背包)
洛谷 P2015 二叉苹果树 (树上背包) 一道树形DP,本来因为是二叉,其实不需要用树上背包来干(其实即使是多叉也可以多叉转二叉),但是最近都刷树上背包的题,所以用了树上背包. 首先,定义状态\(d ...
- centos 7 查看所有登录用户的操作历史
2019-01-07 转自 https://www.cnblogs.com/kevingrace/p/7373146.html centos 7 查看所有登录用户的操作历史 在Linux系统的环境下 ...
- GitHub注册和Git安装
一.注册GitHub GitHub官方地址:https://github.com. 在浏览器中打开GitHub网址,通过首页进行注册,如下图所示. 二.安装Git Git官方下载地址:http://g ...
- WPF中Label使用StringFormat
1. 在WPF中Label的Content有时内容只需要改变个别数字,而不需要所以内容都修改,这时候就要使用StringFormat, 如: <Label Content="I hav ...
- html中设置textbox的宽和高
1.宽:width是不行的,而应该用size size=30,表示能输入30个字符 2.高: style="height:50px"
- 剑指offer(36-40)编程题
两个链表的第一个公共结点 数字在排序数组中出现的次数 二叉树的深度 平衡二叉树 数组中只出现一次的数字 36.输入两个链表,找出它们的第一个公共结点. class Solution1 { public ...