题目传送门

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. 获得WebApi用Post方法获得新增数据的信息

    首先,要知道webApi的基本返回方式是HttpResponseMessage,post会在响应中返回添加的对象,以及添加对象的访问地址 如:在fiddler里测试的时候 然后,我们可以根据这一点在后 ...

  2. 数据结构&图论:LCT

    HDU4010 类比静态区间问题->动态区间问题的拓展 我们这里把区间变成树,树上的写改删查问题,最最最常用LCT解决 LCT用来维护动态的森林,对于森林中的每一棵树,用Splay维护. LCT ...

  3. vijos 1426 背包+hash

    背景 北京奥运会开幕了,这是中国人的骄傲和自豪,中国健儿在运动场上已经创造了一个又一个辉煌,super pig也不例外……………… 描述 虽然兴奋剂是奥运会及其他重要比赛的禁药,是禁止服用的.但是运动 ...

  4. Codeforces 713C Sonya and Problem Wihtout a Legend DP

    C. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

  5. 牛客网刷题(纯java题型 31~60题)

    牛客网刷题(纯java题型 31~60题) 重写Override应该满足"三同一大一小"三同:方法名相同,参数列表相同,返回值相同或者子类的返回值是父类的子类(这一点是经过验证的) ...

  6. Git 常用命令(二)

    用 git init 在目录中创建新的 Git 仓库.  $ mkdir test $ cd test/ $ git init Initialized empty Git repository in ...

  7. web_一些常用的线上脚本地址记录(个人使用)

    1.jquery <script src="http://code.jquery.com/jquery-1.4.1.min.js"></script> 2. ...

  8. Fiddler抓取HTTPS协议

    HTTPS协议握手过程: 1,客户端明文请求,把自己支持的非对称加密算法(用于使用CA证书公钥加密计算生成协商密钥的随机数per_master).对称加密算法(用于以后使用协商密钥加密传输内容).验证 ...

  9. Spark实现销量统计

    package com.mengyao.examples.spark.core; import java.io.Serializable; import org.apache.hadoop.io.Nu ...

  10. python3使用web.py遇到的找不属性的错误解决

    今天用pyhon安装完web.py的时候,点击运行还是没错的,但是在网页输入链接就会报错.1.安装我是这样的: pip install web.py 2.运行后错误信息是这样: AttributeEr ...