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 Vali in 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

  1. 4
  2. 0 77
  3. 1 51
  4. 1 33
  5. 2 69
  6. 4
  7. 0 20523
  8. 1 19243
  9. 1 3890
  10. 0 31492

Sample Output

  1. 77 33 69 51
  2. 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.

Source

 
提交地址 : Buy Tickets
 
题目大意 : 来了n个人, 每个人在队列里选择了一个位置插入, 问最后的序列是什么样的;
 
分析:
时光倒流:因为最后的人肯定是站在自己想占的地方, 所以从后往前便于处理;
首先设一个人都没有, 序列全部为1(后面讲为什么);
然后插入最后一个人, 那样例二来说, 他肯定是插在了自己想在的地方, 所以把他所站的地方设为0, 序序列便成为了 0 1 1 1;
然后考虑倒数第二个人, 他要去第二的位置但是在他之前已经插入了最后一个人, 所以他最终的位置一定不是在第二个位置;
那是在第几个位置呢? 答案是:第二个1!为什么, 因为1表示此位置还未被占, 所以他要站在第二个没有被占得位置上;
求前缀1的值我们可以用树状数组维护(这就是初始值是1的原因!);
但是对于没个人都要枚举一遍显然是不现实的, 又因为前缀和满足单调性, 所以直接暴力二分OK;
 
应该特别好理解;
不理解的看看代码就差不多了;
代码奉上:
 
  1. //By zZhBr
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int n;
  8.  
  9. struct pro
  10. {
  11. int pos;
  12. int num;
  13. }pr[];
  14.  
  15. int ans[];
  16.  
  17. int tr[];
  18.  
  19. int lowbit(int x)
  20. {
  21. return x & -x;
  22. }
  23.  
  24. void add(int x, int y)
  25. {
  26. while(x <= n)
  27. {
  28. tr[x] += y;
  29. x += lowbit(x);
  30. }
  31. }
  32.  
  33. int sum(int x)
  34. {
  35. int ans = ;
  36. while(x != )
  37. {
  38. ans += tr[x];
  39. x -= lowbit(x);
  40. }
  41. return ans;
  42. }
  43.  
  44. int main()
  45. {
  46. while(scanf("%d", &n) != EOF)
  47. {
  48. for(register int i = ; i <= n ; i ++) ans[i] = ;
  49. for(register int i = ; i <= n ; i ++)
  50. {
  51. scanf("%d%d", &pr[i].pos, &pr[i].num);
  52. add(i, );
  53. }
  54.  
  55. for(register int i = n ; i >= ; i --)
  56. {
  57. int p = pr[i].pos + ;
  58.  
  59. if(sum(p) == p)
  60. {
  61. ans[p] = pr[i].num;
  62. add(p, -);
  63. continue;
  64. }
  65.  
  66. int l = p, r = n;
  67. while(l < r)
  68. {
  69. int mid = l + r >> ;
  70. if(sum(mid) >= p) r = mid;
  71. else l = mid + ;
  72. }
  73.  
  74. ans[l] = pr[i].num;
  75. add(l, -);
  76.  
  77. }
  78.  
  79. for(register int i = ; i <= n ; i ++)
  80. {
  81. printf("%d ", ans[i]);
  82. }
  83. printf("\n");
  84.  
  85. }
  86. return ;
  87. }

zZhBr

 
 

POJ2828 Buy Tickets 树状数组的更多相关文章

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

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

  2. POJ2828 Buy Tickets [树状数组,二分答案]

    题目传送门 Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 22611   Accepted: 110 ...

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

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

  4. poj 2828 Buy Tickets 树状数组

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

  5. H - Buy Tickets POJ - 2828 逆序遍历 树状数组+二分

    H - Buy Tickets POJ - 2828 这个题目还是比较简单的,其实有思路,不过中途又断了,最后写了一发别的想法的T了. 然后脑子就有点糊涂,不应该啊,这个题目应该会写才对,这个和之前的 ...

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

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

  7. POJ 2828 Buy Tickets (线段树 || 树状数组)

    题目大意 一些小朋友在排队,每次来一个人,第i个人会插到第x个人的后面.权值为y.保证x∈[0,i-1]. 按照最后的队伍顺序,依次输出每个人的权值. 解题分析 好气吖.本来是在做splay练习,然后 ...

  8. 【poj2182】【poj2828】树状数组/线段树经典模型:逆序查找-空位插入法

    poj2182题意:有一个1~n的排列,现在给定每个人前面有多少个人的编号比他大,求这个排列是什么.n<=8000 poj2182题解: 逆序做,可以确定二分最后一个是什么,然后删除这个数.树状 ...

  9. 小结:线段树 & 主席树 & 树状数组

    概要: 就是用来维护区间信息,然后各种秀智商游戏. 技巧及注意: 一定要注意标记的下放的顺序及影响!考虑是否有叠加或相互影响的可能! 和平衡树相同,在操作每一个节点时,必须保证祖先的tag已经完全下放 ...

随机推荐

  1. Docker Compose部署项目到容器-基于Tomcat和mysql的商城项目(附源码和sql下载)

    场景 Docker-Compose简介与Ubuntu Server 上安装Compose: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  2. Sqoop介绍、安装与操作

    搭建环境 部署节点操作系统为CentOS,防火墙和SElinux禁用,创建了一个shiyanlou用户并在系统根目录下创建/app目录,用于存放 Hadoop等组件运行包.因为该目录用于安装hadoo ...

  3. ACM团队周赛题解(3)

    940和822两套div.2 老规矩 #define MAXN 1000000+5#define MOD 1000000007#define PI (acos(-1.0))#define EPS 1e ...

  4. 02.Django基础二之URL路由系统

    一 URL配置 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表.你就是以这 ...

  5. 21 (OC) 数据持久化

    概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方案: plist文件(属性列表) ...

  6. GDB 调试指南

    本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复 「1024」 即可领取,欢迎大家关注,二维码文末可以扫. 00 介绍 ...

  7. 在.NET Core中使用DispatchProxy“实现”非公开的接口

    原文地址:"Implementing" a non-public interface in .NET Core with DispatchProxy 原文作者:Filip W. 译 ...

  8. Spring Boot (四): Druid 连接池密码加密与监控

    在上一篇文章<Spring Boot (三): ORM 框架 JPA 与连接池 Hikari> 我们介绍了 JPA 与连接池 Hikari 的整合使用,在国内使用比较多的连接池还有一个是阿 ...

  9. jenkins自动化部署项目6 --STMP服务发送邮件配置

    [系统管理]-[系统设置]里 1.系统管理员邮件地址:和后面SMTP配置认证的邮箱要一致,即发件箱   --------切记 2.配置SMTP认证的前提是需要在sohu搜狐邮箱开通SMTP/POP3, ...

  10. 关于WinForm TreeView的分享~

    最近在写个测试demo的时候使用到WinForm TreeView,已经好久没接触了,有些生疏,所以还是记录一下遇到的一些问题. 1.如果动态绑定TreeView,这个功能一般会在数据量不确定,需要去 ...