POJ_2828 Buy Tickets 【线段树】
一、题目
二、分析
首先可以明确的是每个人的位置都是定的,那么如果从输入数据从后往前看,最后面的人进来的时候,他前面的人数肯定是定的。
那么可以考虑,当从后往前推时,这个人插入的位置就是他前面有多少空位,假设他的位置比空位数少,那显然是不可以的,如果他的位置比空位多,那么后面的已经插入的,没有人来补这个位置了,所以显然也不合理,所以假设区间$[1,t]$刚好有$pos+1$个空位,那么它的位置应该就在最后一个空位处。
三、AC代码
1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 #include <algorithm>
5 #include <vector>
6 #include <cmath>
7
8 using namespace std;
9 #define ll long long
10 #define Min(a,b) ((a)>(b)?(b):(a))
11 #define Max(a,b) ((a)>(b)?(a):(b))
12 #define P pair<int, int>
13 #define lson (rt<<1)
14 #define rson (rt<<1|1)
15 const int MAXN = 2e5;
16 int ans[MAXN + 13];
17 int sum[MAXN<<2]; //表示区间内还有多少个位置
18
19 void Build(int rt, int l, int r)
20 {
21 sum[rt] = r - l + 1;
22 if(l == r) {
23 sum[rt] = 1;
24 return;
25 }
26 int mid = (l + r) >> 1;
27 Build(lson, l, mid);
28 Build(rson, mid + 1, r);
29 }
30
31 void Update(int rt, int l, int r, int pos, int val)
32 {
33 if(l == r) {
34 sum[rt] = 0;
35 ans[l] = val;
36 return;
37 }
38 int mid = (l + r) >> 1;
39 if(pos <= sum[lson])
40 Update(lson, l, mid, pos, val);
41 else
42 Update(rson, mid + 1, r, pos - sum[lson], val);
43 sum[rt]--;
44 }
45
46 int main()
47 {
48 //freopen("input.txt", "r", stdin);
49 int N;
50 while(scanf("%d", &N) != EOF) {
51 int a, b;
52 Build(1, 1, N);
53 vector<P> vec;
54 for(int i = 0; i < N; i++) {
55 scanf("%d%d", &a, &b);
56 vec.push_back(P(a, b));
57 }
58 for(int i = vec.size()-1; i >= 0; i--) {
59 Update(1, 1, N, vec[i].first + 1, vec[i].second);
60 }
61 for(int i = 1; i <= N; i++) {
62 printf("%d%c", ans[i], i == N ? '\n' : ' ');
63 }
64
65 }
66 return 0;
67 }
POJ_2828 Buy Tickets 【线段树】的更多相关文章
- [poj2828] Buy Tickets (线段树)
线段树 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must ...
- 【poj2828】Buy Tickets 线段树 插队问题
[poj2828]Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in ...
- poj 2828 Buy Tickets (线段树(排队插入后输出序列))
http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissio ...
- POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19725 Accepted: 9756 Desc ...
- Buy Tickets(线段树)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 16607 Accepted: 8275 Desc ...
- poj-----(2828)Buy Tickets(线段树单点更新)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 12930 Accepted: 6412 Desc ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- poj2828 Buy Tickets (线段树 插队问题)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 22097 Accepted: 10834 Des ...
- POJ 2828 Buy Tickets | 线段树的喵用
题意: 给你n次插队操作,每次两个数,pos,w,意为在pos后插入一个权值为w的数; 最后输出1~n的权值 题解: 首先可以发现,最后一次插入的位置是准确的位置 所以这个就变成了若干个子问题, 所以 ...
- POJ 2828 Buy Tickets(线段树·插队)
题意 n个人排队 每一个人都有个属性值 依次输入n个pos[i] val[i] 表示第i个人直接插到当前第pos[i]个人后面 他的属性值为val[i] 要求最后依次输出队中各个人的属性 ...
随机推荐
- 获取txt编码方式
在操作txt的时候,有时会出现乱码,这是因为没有使用正确的编码方式来操作txt,我们需要先获取txt的编码方式,再进行读写操作.下面是获取txt编码的方法: /// <summary> / ...
- Redis 持久化(Persistence)
作为内存数据库,Redis 依然提供了持久化机制,其主要目的有两个: 安全:保证进程崩溃后数据不会丢失 备份:方便数据迁移与快速恢复 Redis 同时提供两种持久化机制: RDB 快照:数据库在某个时 ...
- test markdown && 代码高亮
#include<cstdio> #include<cstring> #include<queue> #include<vector> #include ...
- zoj-3872 Beauty of Array (dp)
]Edward has an array A with N integers. He defines the beauty of an array as the summation of all di ...
- 探究为什么FreeRTOS 有些API不能在中断服务函数中调用,转而需要调用带ISR的版本
用了好久的FreeRTOS以前只是知道,如果在中断服务程序中调用某一些FreeRTOS的API函数时需要注意,如果有ISR版本的一定要调用末尾带ISR的函数,并且中断服务程序要调用freeRTOS的A ...
- SQL优化这么做就对了
目录 前言 SQL优化一般步骤 1.通过慢查日志等定位那些执行效率较低的SQL语句 2.explain 分析SQL的执行计划 3.show profile 分析 4.trace 5.确定问题并采用相应 ...
- Promise nested then execute order All In One
Promise nested then execute order All In One Promise nested then nested Promise not return new Promi ...
- I ❤️ W3C : Secure Contexts
I ️ W3C : Secure Contexts Secure Contexts W3C Candidate Recommendation, 15 September 2016 https://ww ...
- axios upload excel file
axios upload excel file https://github.com/axios/axios/issues/1660 https://stackoverflow.com/questio ...
- input support upload excel only
input support upload excel only demo https://codepen.io/xgqfrms/pen/vYONpLB <!-- <input placeh ...