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——线段树的其他信息维护的更多相关文章

  1. poj 2828 Buy Tickets (线段树(排队插入后输出序列))

    http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissio ...

  2. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  3. POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19725   Accepted: 9756 Desc ...

  4. POJ 2828 Buy Tickets | 线段树的喵用

    题意: 给你n次插队操作,每次两个数,pos,w,意为在pos后插入一个权值为w的数; 最后输出1~n的权值 题解: 首先可以发现,最后一次插入的位置是准确的位置 所以这个就变成了若干个子问题, 所以 ...

  5. POJ 2828 Buy Tickets(线段树&#183;插队)

    题意  n个人排队  每一个人都有个属性值  依次输入n个pos[i]  val[i]  表示第i个人直接插到当前第pos[i]个人后面  他的属性值为val[i]  要求最后依次输出队中各个人的属性 ...

  6. POJ 2828 Buy Tickets(线段树单点)

    https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的 ...

  7. poj 2828 Buy Tickets (线段树)

    题目:http://poj.org/problem?id=2828 题意:有n个人插队,给定插队的先后顺序和插在哪个位置还有每个人的val,求插队结束后队伍各位置的val. 线段树里比较简单的题目了, ...

  8. Buy Tickets 【POJ - 2828】【线段树】

    题目链接 有N次操作,每次都是将第i个数放置在第pos个数的后面,并且这个数的值是val. 这个线段树的思维确实很好,我们可以发现,后面放进去的数,一定是强制位置的,而前面放的数,会随着后面的数进入而 ...

  9. [poj2828] Buy Tickets (线段树)

    线段树 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must ...

随机推荐

  1. canvas+js画饼状图

    效果: 源码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  2. Luogu P1351 联合权值 题解

    这是一个不错的树形结构的题,由于本蒟蒻不会推什么神奇的公式其实是懒得推...,所以很愉快的发现其实只需要两个点之间的关系为祖父和儿子.或者是兄弟即可. 然后问题就变得很简单了,只需要做一个正常的DFS ...

  3. docker 限制 容器内存 使用

    转载 : https://www.cnblogs.com/sparkdev/p/8032330.html 默认情况下容器使用的资源是不受限制的.也就是可以使用主机内核调度器所允许的最大资源.但是在容器 ...

  4. 【笔记】AJAX基础

    [笔记]AJAX基础 Django AJAX  知识储备:JSON 什么是 JSON JSON 是轻量级的文本数据交换格式 JSON 独立于语言和平台.JSON 解析器和 JSON 库支持许多不同的编 ...

  5. P3440 [POI2006]SZK-Schools

    传送门 应该是很显然的费用流模型吧... $S$ 向所有学校连边,流量为 $1$,费用为 $0$(表示每个学校要选一个编号) 学校向范围内的数字连边,流量为 $1$,费用为 $c|m-m'|$(表示学 ...

  6. [意识流]简单易懂的AC自动机

    为了一言不合就徒手敲AC自动机,决定看一下原理 于是花了一张图, 参考HDU2222的样例 于是看懂这张图的你很快就敲出了如下代码并且AC了 #include<bits/stdc++.h> ...

  7. 检查SQL语句是否合法

    昨天又有一个新的需求:验证文本框输入的SQL语法是否正确. 于是就开始百度,其实也挺简单的. 首先需要知道“SET PARSEONLY { ON | OFF }”. 当 SET PARSEONLY 为 ...

  8. (7.0 version)当销售单中包含service或phantom类型的产品时,销售单不能完成的原因分析及解决方案

    首先说一下service类型的产品,由于该类型的产品不需要发货,所以当在销售订单确认了后,销售单直接变成了等待开票的状态,但当开票的流程结束后,订单却还是停在销售单的状态上,该问题的解决方案是安装Ta ...

  9. Mac 10.12通过Launchd创建自定义服务(基于MySQL 5.7.15的开机自启动)

    在上一篇文章http://www.cnblogs.com/EasonJim/p/6275863.html中安装MySQL时采用的时DMG包的安装步骤页面进行安装的,如果这样安装的MySQL是会开机自启 ...

  10. css hack 汇整

    针对Chrome和Safari等Webkit核心浏览器的CSS hack代码: @media screen and (-webkit-min-device-pixel-ratio:0) { …… } ...