(中等) POJ 2828 Buy Tickets , 逆序+线段树。
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 , 逆序+线段树。的更多相关文章
- POJ 2828.Buy Tickets-完全版线段树(单点更新、逆序遍历查询)
POJ2828.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.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- 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(线段树单点更新)
http://poj.org/problem?id=2828 题意 排队买票,依次给出当前人要插队的位置,每个人有个编号,然后问你最后整个的序列是什么? 分析 最后一个人的要插入的位置是确定的,所以逆 ...
随机推荐
- 1.1 selenium 安装
1.在火狐浏览器的附加组件里搜索Selenium IDE
- 3个同一行div的摆放div
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Swift POP+MVVM
Swift2.0中引入了协议扩展的特性,并且建议开发者一切从协议(Protocol)出发,经过几个月的学习探索,博主发现Swift作为一门面向协议编程(POP)的语言非常适合时下火热的MVVM架构.M ...
- PullToRefreshListView上拉加载、下拉刷新
说明:此项目使用studio完成的.需要导入library作为依赖,使用了xuitls获得网络请求.使用Pull解析了XML eclipse中的项目: //注意:此刷新功能是使用的第三方的PullTo ...
- Git学习 -- 搭建Git服务器
搭建环境 服务器端:CentOS 6.5 IP:192.168.101.129 客户端:CentOS 6.5 . Windows 服务器端: 创建repository版本库,例如/srv/test ...
- JAVA动态代理详解
1.什么是代理 代理模式是常用的Java 设计模式,它的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等. 2.什么是动态代理 在程 ...
- MFC实现为窗体添加的背景图片
将一个bmp图片添加到资源中 在资源视图中更改位图资源的ID为IDB_BITMAP_BACKGROUND. 第一种方法: 在Dialog中添加一个Picture Control控件,将Picture ...
- 第13章 Swing程序设计----常用面板
面板也是一个Swing容器,它可以作为容器容纳其他组件,但它也必须被添加到其他容器中. Swing常用的面板包括JPanel面板和JScrollPanel面板. 1.JPanel面板 import j ...
- FragmentActivity
子fragment 调用 FragmentActivity ((FragmentActivity) getActivity()).updateUnreadLabel(); FragmentActivi ...
- Super Mario
Super Mario Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...