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.

  就是说几个人在排队,有人插队,然后问最后的队列是啥样。

  很经典的问题(可是我没想出来。T T)。

  从后往前枚举,建树。 首先最后一个人在哪就是哪,那么他前面的人如果原来在的位置在最后一个人插入的位置之后,那么就要向后移一个位置。

  所以说就是到了某个人时(倒序枚举),假设他要插在3这个位置,那么第三个空的地方(已经有人的不计数)就是他的位置。

  代码如下:

#include<iostream>
#include<cstdio> using namespace std; int BIT[*];
int N;
int num[];
int val[];
int ans[]; void build_tree(int L,int R,int po)
{
BIT[po]=R-L+; if(L==R) return; int M=(L+R)/; build_tree(L,M,po*);
build_tree(M+,R,po*+);
} int query_update(int qn,int L,int R,int po)
{
--BIT[po];
if(L==R) return L; int M=(L+R)/; if(BIT[po*]>=qn)
return query_update(qn,L,M,po*);
else
return query_update(qn-BIT[po*],M+,R,po*+);
} int main()
{
int temp; while(~scanf("%d",&N))
{
for(int i=N;i>=;--i)
scanf("%d %d",&num[i],&val[i]); build_tree(,N,); for(int i=;i<=N;++i)
{
temp=query_update(num[i]+,,N,);
ans[temp]=val[i];
} for(int i=;i<=N;++i)
printf("%d ",ans[i]);
printf("\n");
} return ;
}

(中等) POJ 2828 Buy Tickets , 逆序+线段树。的更多相关文章

  1. POJ 2828.Buy Tickets-完全版线段树(单点更新、逆序遍历查询)

    POJ2828.Buy Tickets 这个题是插队问题,每次有人插队的时候,其后的所有数据都要进行更新,如果我们反着推,就可以把所有的数据都安排好并且不用再对已插入的数据进行更新,因为逆序处理的话所 ...

  2. POJ 2828 Buy Tickets(排队问题,线段树应用)

    POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意:  排队买票时候插队.  给出一些数对,分别代表某个人的想要插入的位 ...

  3. poj 2828 Buy Tickets(树状数组 | 线段树)

    题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...

  4. poj 2828 Buy Tickets 【线段树点更新】

    题目:id=2828" target="_blank">poj 2828 Buy Tickets 题意:有n个人排队,每一个人有一个价值和要插的位置,然后当要插的位 ...

  5. 线段树(单点更新) POJ 2828 Buy tickets

    题目传送门 /* 结点存储下面有几个空位 每次从根结点往下找找到该插入的位置, 同时更新每个节点的值 */ #include <cstdio> #define lson l, m, rt ...

  6. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

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

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

  8. poj 2828 Buy Tickets【线段树单点更新】【逆序输入】

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 16273   Accepted: 8098 Desc ...

  9. POJ - 2828 Buy Tickets(线段树单点更新)

    http://poj.org/problem?id=2828 题意 排队买票,依次给出当前人要插队的位置,每个人有个编号,然后问你最后整个的序列是什么? 分析 最后一个人的要插入的位置是确定的,所以逆 ...

随机推荐

  1. VS2010+PCL+openni配置

    PCL中文论坛:http://www.pclcn.org/bbs/forum.php 1.安装 pcl 的完全安装包可以到: http://pointclouds.org/downloads/wind ...

  2. PHP导出Excel 数字末尾变0或小数点解决办法

    为了管理方便,查阅直观性.很多网站, 或者系统都会自带导出excel或者cvs的功能.但是很多情况下,由于数字超过15位,会被显示成0或者加小数点处理.造成这种情况是由于Excel内 置的数值有效范围 ...

  3. 下拉刷新和上拉加载 Swift

    转载自:http://iyiming.me/blog/2015/07/05/custom-refresh-and-loading/ 关于下拉刷新和上拉加载,项目中一直使用MJRefresh(原先还用过 ...

  4. hdu 3345 War Chess

    War Chess Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  5. 优化のzencart URL &zenid=.....

    zencart URL后面带有一串&zenid=.....解决方案 发布时间:2013年3月16日 次浏览:106 经木木测试,此方法可用. ================= 最近一个客户的 ...

  6. P2P应用中的NAT穿透问题

    多年前曾经写过一个关于NAT钻洞的实验.现在发现那个做法在我现在的路由器上已经不管用了.经过一番搜索发现时过境迁,世界变化很快,新路由器已经是UPnP了.在这里重新理一下几种方法. 第一种,也是不太靠 ...

  7. CentOS添加字体

    到Windows XP或者Vista下复制字体到CentOS 1.到Windows XP或者Vista下复制字体到CentOS 雅黑:msyh 黑体:SimHei 宋体:SimSun 华文细黑:STX ...

  8. PD生成oracle表名带引号解决方案

    使用PowerDesigner生成数据库建表SQL脚本时,尤其是Oracle数据库时,表名一般会带引号.其实加引号是PL/SQL的规范,数据库会 严格按照“”中的名称建表,如果没有“”,会按照ORAC ...

  9. 代码异味---Code smell

    程序员应该竭尽全力去写那些重复的代码.以下几点是我目前最需要改进的地方. 重复代码: 相同或者相似的代码存在于一个以上的地方. 长方法: 一个非常长的方法.函数或者过程. 巨类: 一个非常庞大的类. ...

  10. ButterKnife-5.1.2.jar(较低版本的ButterKnife)使用方法

    ButterKnife注解是编译时注解,Xutils是运行时注解.很明显编译时注解性能更高 一.先配置好环境 1.在工程上右键,选Properties,然后选Java Compiler,选Annota ...