Buy Tickets(poj2828)
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 17416 | Accepted: 8646 |
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
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
思路:线段树。
其实就是找第K大的数,被这题坑了好多时间,本来开始咋都想不通。本来打算放弃的。
下面说一下:Posi ∈ [0, i − 1]这个条件很关键。我们从最后一个开始选取,最后一个是很容易定的,因为它就是当前所有数的第aa[i].x+1大的位置上,可以把他看成第aa[i].x+1大的数,
那么当最后一个选完后,倒数第二个就可以看成最后一个了,那么倒数第二个就是当前剩余位置中排序第aa[i-1].x+1的位置上,然后这样选取到最后就行了。其中选取的过程用线段树维护就行。复杂度N*log(n)*log(n);
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<math.h>
5 #include<stdlib.h>
6 #include<string.h>
7 using namespace std;
8 int tree[5*200000];
9 int id[200005];
10 typedef struct pp
11 {
12 int x;
13 int y;
14 } ss;
15 ss num[200005];
16 int id1[200005];
17 int ask(int l,int r,int k,int ans);
18 void build(int l,int r,int k);
19 void up(int k);
20 int main(void)
21 {
22 int i,j,k,p,q;
23 while(scanf("%d",&k)!=EOF)
24 { memset(tree,0,sizeof(tree));
25 memset(id1,0,sizeof(id1));
26 for(i=0; i<k; i++)
27 {
28 scanf("%d %d",&num[i].x,&num[i].y);
29 num[i].x+=1;
30 }
31 build(0,k-1,0);
32 for(i=k-1; i>=0; i--)
33 {
34 int er=ask(0,k-1,0,num[i].x);
35 id1[er]=num[i].y;
36 }
37 printf("%d",id1[0]);
38 for(i=1; i<k; i++)
39 printf(" %d",id1[i]);
40 printf("\n");
41 }
42 return 0;
43 }
44 void build(int l,int r,int k)
45 {
46 if(l==r)
47 {
48 tree[k]=1;
49 id[l]=k;
50 return ;
51 }
52 else
53 {
54 build(l,(l+r)/2,2*k+1);
55 build((l+r)/2+1,r,2*k+2);
56 tree[k]=tree[2*k+1]+tree[2*k+2];
57 }
58 }
59 void up(int k)
60 {
61 tree[k]=0;
62 if(k==0)return ;
63 else
64 {
65 int cc=k;
66 cc=(cc-1)/2;
67 while(cc>=0)
68 {
69 tree[cc]=tree[2*cc+1]+tree[2*cc+2];
70 if(cc==0)
71 return ;
72 cc=(cc-1)/2;
73 }
74 }
75 }
76 int ask(int l,int r,int k,int ans)
77 {
78 if(ans==tree[k])
79 {
80 int c=id[r];
81 if(tree[c]==1)
82 {
83 up(c);
84 return r;
85 }
86 else
87 {
88 if(tree[2*k+1]<ans)
89 {
90 return ask((l+r)/2+1,r,2*k+2,ans-tree[2*k+1]);
91 }
92 else if(tree[2*k+1]==ans)
93 {
94 return ask(l,(l+r)/2,2*k+1,ans);
95 }
96 }
97 }
98 else if(ans<tree[k])
99 {
100 if(tree[2*k+1]>=ans)
101 {
102 return ask(l,(l+r)/2,2*k+1,ans);
103 }
104 else if(tree[2*k+1]<ans)
105 {
106 return ask((l+r)/2+1,r,2*k+2,ans-tree[2*k+1]);
107 }
108 }
109 }
Buy Tickets(poj2828)的更多相关文章
- 【poj2828】Buy Tickets 线段树 插队问题
[poj2828]Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in ...
- [POJ2828] Buy Tickets(待续)
[POJ2828] Buy Tickets(待续) 题目大意:多组测试,每组给出\(n\)条信息\((a,b)\),表示\(b\)前面有\(a\)个人,顺序靠后的信息优先级高 Solution.1 由 ...
- POJ2828 Buy Tickets[树状数组第k小值 倒序]
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19012 Accepted: 9442 Desc ...
- poj-----(2828)Buy Tickets(线段树单点更新)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 12930 Accepted: 6412 Desc ...
- POJ2828 Buy Tickets 【线段树】+【单点更新】+【逆序】
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 12296 Accepted: 6071 Desc ...
- poj2828 Buy Tickets (线段树 插队问题)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 22097 Accepted: 10834 Des ...
- poj-2828 Buy Tickets(经典线段树)
/* Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 10207 Accepted: 4919 Descr ...
- POJ2828 Buy Tickets [树状数组,二分答案]
题目传送门 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 22611 Accepted: 110 ...
- POJ2828 Buy Tickets 树状数组
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
随机推荐
- 详解工作流框架Activiti的服务架构和组件
摘要:通过这篇文章,可以对工作流有一个基本的认识,为后续工作流框架Activiti的学习打下坚实的基础. 本文分享自华为云社区<BPMN工作流的基本概念!详解工作流框架Activiti的服务架构 ...
- python函数初体验
函数 函数参数w 形式参数>>>>(被指定具体的值)默认参数, 实际参数是调用时候的实际指定参数 我们把函数⾥⾯的参数叫形式函数,函数实际调⽤的时候,赋予的参数叫实际函数 定义 ...
- 大数据学习day35----flume01-------1 agent(关于agent的一些问题),2 event,3 有关agent和event的一些问题,4 transaction(事务控制机制),5 flume安装 6.Flume入门案例
具体见文档,以下只是简单笔记(内容不全) 1.agent Flume中最核心的角色是agent,flume采集系统就是由一个个agent连接起来所形成的一个或简单或复杂的数据传输通道.对于每一个Age ...
- Activity 详解
1.活动的生命周期 1.1.返回栈 Android是使用任务(Task)来管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈也被称作返回栈.栈是一种先进后出的数据结构,在默认情况下,每当我们启 ...
- Linux lvm在线扩容
1.查看磁盘空间 [root@bgd-mysql3 ~]# fdisk -l Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sector ...
- 用oracle中的Row_Number实现分页
Row_Number实现分页 1:首先是 select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1 生成带序号 ...
- 【Java 8】Stream中的Pipeline理解
基于下面一段代码: public static void main(String[] args) { List<String> list = Arrays.asList("123 ...
- Spring Boot的异步任务、定时任务和邮件任务
一.异步任务 1.启动类添加注解@EnableAsync,开启异步任务注解功能: 2.需要异步执行的方法上添加@Async注解. 二.定时任务 1.启动类添加注解@EnableScheduling,开 ...
- Java poi导出设置 Excel某些单元格不可编辑
小白的总结,大神勿喷:需要转载请说明出处,如果有什么问题,欢迎留言 一.需求: 1.某一列 .某一行或某些单元格不可编辑,其他列可以编辑 二.期间遇到的问题 1.无法设置成不可编辑 2.设置为不可编辑 ...
- 【力扣】剑指 Offer 50. 第一个只出现一次的字符
在字符串 s 中找出第一个只出现一次的字符.如果没有,返回一个单空格. s 只包含小写字母. 示例: s = "abaccdeff"返回 "b" s = &qu ...