题目传送门

Buy Tickets

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 22611   Accepted: 11045

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.

Input

There will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posi and Valiin the increasing order of i (1 ≤ i ≤ N). For each i, the ranges and meanings of Posi and Vali are as follows:

  • Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th person in the queue. The booking office was considered the 0th person and the person at the front of the queue was considered the first person in the queue.
  • Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.

There no blank lines between test cases. Proceed to the end of input.

Output

For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.

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.


  分析:

  首先分析,直接暴力模拟是O(n^2)的复杂度,肯定不行,那么换一种思维,逆向进行。很明显,第n个人只要放在n个位置中的第p[n]的位置就行了,那么可以推出,第n-1个人应该放在剩下的n-1个位置中的第p[n-1]个位置就行了,依次类推,那么问题就转换成求i个位置中的第p[i]个位置在哪里。这里可以用线段树也可以用树状数组。蒟蒻用的是树状数组+二分,打的不好看将就下吧。

  Code:

//It is made by HolseLee on 12th June 2018
//POJ 2828
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std;
const int N=2e5+;
int n,c[N],ans[N];
struct Man{int pos,id,val;}a[N];
inline int read()
{
char ch=getchar();int num=;bool flag=false;
while(ch<''||ch>''){if(ch=='-')flag=true;ch=getchar();}
while(ch>=''&&ch<=''){num=num*+ch-'';ch=getchar();}
return flag?-num:num;
}
inline int lowbit(int x)
{return x&-x;}
inline void add(int x,int y)
{
for(;x<=n;x+=lowbit(x))c[x]+=y;
}
inline int quary(int x)
{
int ret=;
for(;x>;x-=lowbit(x))ret+=c[x];
return ret;
}
inline int get(int x)
{
int l=,r=n,mid;
while(l<=r){
mid=(l+r)>>;
if(quary(mid)>=x)r=mid-;
else l=mid+;}
return l;
}
int main()
{
while(scanf("%d",&n)!=EOF){
memset(c,,sizeof(c));
memset(ans,,sizeof(ans));
for(int i=;i<=n;i++)add(i,);
for(int i=;i<=n;i++){
a[i].pos=read()+;
a[i].val=read();}
for(int i=n;i>=;i--){
int ka=get(a[i].pos);
ans[ka]=a[i].val;
add(ka,-);}
for(int i=;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[n]);
}
return ;
}

POJ2828 Buy Tickets [树状数组,二分答案]的更多相关文章

  1. POJ2828 Buy Tickets 树状数组

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

  2. POJ2828 Buy Tickets[树状数组第k小值 倒序]

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19012   Accepted: 9442 Desc ...

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

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

  4. 树状数组+二分答案查询第k大的数 (团体程序设计天梯赛 L3-002. 堆栈)

    前提是数的范围较小 1 数据范围:O(n) 2 查第k大的数i:log(n)(树状数组查询小于等于i的数目)*log(n)(二分找到i) 3 添加:log(n) (树状数组) 4 删除:log(n) ...

  5. poj 2828 Buy Tickets 树状数组

    Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in China, so ...

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

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

  7. TZOJ 4602 高桥和低桥(二分或树状数组+二分)

    描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...

  8. The Stream of Corning 2( 权值线段树/(树状数组+二分) )

    题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...

  9. POJ 2182 Lost Cows 【树状数组+二分】

    题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. IE8动态创建CSS

    IE8动态创建CSS 最近在项目中用到在页面中动态创建CSS方法,记录一下方便以后查看 一. 在IE下动态创建(网上收集3种方法,最后一个方法未测试成功,具体不知道什么原因) 第一种(此方法很麻烦,需 ...

  2. 2017 济南综合班 Day 1

    送分题(songfen) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK喜欢干一些有挑战的事,比如说求区间最大子段和.它知道这个题目有O(n)的做法.于 ...

  3. kafka命令总结

    ./kafka-console-consumer.sh  --bootstrap-server IP:9092  --topic user-asset-change-v1 --partition 2  ...

  4. 【BZOJ】1776: [Usaco2010 Hol]cowpol 奶牛政坛

    [题意]给定n个点的树,每个点属于一个分类,求每个分类中(至少有2个点)最远的两点距离.n<=200000 [算法]LCA [题解]结论:树上任意点集中最远的两点一定包含点集中深度最大的点(求树 ...

  5. 【CodeForces】835D Palindromic characteristics

    [题意]给你一个串,让你求出k阶回文子串有多少个.k从1到n.k阶子串的定义是:子串本身是回文串,而且它的左半部分也是回文串. [算法]区间DP [题解]涉及回文问题的区间DP都可以用类似的写法,就是 ...

  6. HDU 2639 Bone Collector II (dp)

    题目链接 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in ...

  7. 以下suse11.3x64可以安装pycrypto-2.6.1

    rpm -qa adaptec-firmware-1.35-2.15.4gnome-menus-branding-SLED-11.1-14.26man-pages-3.15-2.23.1crackli ...

  8. centos_7.1.1503_src_3

    http://vault.centos.org/7.1.1503/os/Source/SPackages/ kdf-4.10.5-3.el7.src.rpm 05-Jul-2014 13:28 161 ...

  9. 【bzoj4486】【JSOI2015】串分割

    老省选题了. 首先考虑怎么比较超长数字的大小? 参见UTR1的那道题 先比size,然后比较字典序即可. 接下来考虑下切割的问题. 因为要将字符串切割成k份,所以这个字符串只会存在n/k个本质不同的起 ...

  10. C json实战引擎 三 , 最后实现部分辅助函数

    引言 大学读的是一个很时髦的专业, 学了四年的游戏竞技. 可惜没学好. 但认真过, 比做什么都认真. 见证了  ...... 打的所有游戏人物中 分享一位最喜爱 的 “I've been alone ...