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这个位置,那么第三个空的地方(已经有人的不计数)就是他的位置。

  代码如下:

  1. #include<iostream>
  2. #include<cstdio>
  3.  
  4. using namespace std;
  5.  
  6. int BIT[*];
  7. int N;
  8. int num[];
  9. int val[];
  10. int ans[];
  11.  
  12. void build_tree(int L,int R,int po)
  13. {
  14. BIT[po]=R-L+;
  15.  
  16. if(L==R) return;
  17.  
  18. int M=(L+R)/;
  19.  
  20. build_tree(L,M,po*);
  21. build_tree(M+,R,po*+);
  22. }
  23.  
  24. int query_update(int qn,int L,int R,int po)
  25. {
  26. --BIT[po];
  27. if(L==R) return L;
  28.  
  29. int M=(L+R)/;
  30.  
  31. if(BIT[po*]>=qn)
  32. return query_update(qn,L,M,po*);
  33. else
  34. return query_update(qn-BIT[po*],M+,R,po*+);
  35. }
  36.  
  37. int main()
  38. {
  39. int temp;
  40.  
  41. while(~scanf("%d",&N))
  42. {
  43. for(int i=N;i>=;--i)
  44. scanf("%d %d",&num[i],&val[i]);
  45.  
  46. build_tree(,N,);
  47.  
  48. for(int i=;i<=N;++i)
  49. {
  50. temp=query_update(num[i]+,,N,);
  51. ans[temp]=val[i];
  52. }
  53.  
  54. for(int i=;i<=N;++i)
  55. printf("%d ",ans[i]);
  56. printf("\n");
  57. }
  58.  
  59. return ;
  60. }

(中等) 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. 2.2 sikuli中编程运行

    http://www.cnblogs.com/Flint/p/4951703.html a.如果需要指定点击的具体坐标,需要使用click(patten.targetoffset(x, y)). b. ...

  2. WPF(x:Null 使用)

    <Window x:Class="TestOfNull.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...

  3. centos yum源问题

    在配置CentOS-6.0-x86_64-bin-DVD2.iso作为本地yum源的时候,碰到相当多的问题: -----------------------------------------  问题 ...

  4. java 接口参数

    Example6_5.java interface SpeakHello { void speakHello(); } class Chinese implements SpeakHello { pu ...

  5. NPOI 2.0 教程(二):编辑既存的EXCEL文件

    NPOI 2.0 教程(二):编辑既存的EXCEL文件 分类: C#技术 2014-03-11 15:40 993人阅读 评论(3) 收藏 举报 c#excelNPOI 转载请注明出处 http:// ...

  6. jq模拟操作

    1.常用模拟 trigger() $('#btn').trigger('click'); 当页面加载完,点击事件就会完成 上面也可以简写成:$('#btn').click(); 2.触发自定义事件 t ...

  7. Eclipse配置

    下载地址:http://www.eclipse.org/downloads/ tomcat plugin:http://www.eclipsetotale.com/tomcatPlugin.html# ...

  8. C#中String和stringBuilder的区别

    Stringbuilder类是直接用于字符串操作的类,打个比方把(1)string aa="123456";(2)aa+="789"; (3)StringBui ...

  9. 12C 连接方式和 Oracle Easy Connect Naming method

    1.12C 连接方式 PDB is not an instance, so using SID in the connection string will not work. When the dat ...

  10. CodeForces 158C - Cd and pwd commands(模拟)

    这个题我们又把题意理解错了,队友翻译了以后给我解释,我问这个直接一个单词开头的是要找到这个文件夹吗,他说是,然后我就呵呵了..奔着树形结构去和字符串维护就去了...做了好久都没模拟出来,感觉做出来的人 ...