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] 要求最后依次输出队中各个人的属性 ...
随机推荐
- 鸟哥的linux私房菜——第十六章学习(程序管理与 SELinux 初探)
第十六章.程序管理与 SE Linux 初探 在 Linux 系统当中:"触发任何一个事件时,系统都会将他定义成为一个程序,并且给予这个程序一个 ID ,称为 PID,同时依据启发这个程序的 ...
- flagcounter 被禁用...
源地址 https://s11.flagcounter.com/count2/arWz/bg_FFFFFF/txt_000000/border_CCCCCC/columns_2/maxflags_14 ...
- Java开发工程师最新面试题库系列——集合部分(附答案)
集合 如果你有更好的想法请在评论区留下您的答案,一起交流讨论 说说常见的集合有哪些? 答:主要分List.Set.Map.Queue四类,其中包含ArrayList.LinkedList.HashSe ...
- 高并发之Semaphore、Exchanger、LockSupport
本系列研究总结高并发下的几种同步锁的使用以及之间的区别,分别是:ReentrantLock.CountDownLatch.CyclicBarrier.Phaser.ReadWriteLock.Stam ...
- MBP & battery
MBP & battery 实际:3 + 1 个小时左右 4 个小时左右 shit apple 10 小时 Chrome bug https://appleinsider.com/articl ...
- nasm astrlwr_s函数 x86
xxx.asm %define p1 ebp+8 %define p2 ebp+12 %define p3 ebp+16 section .text global dllmain export ast ...
- [Python] 基于 jieba 的中文分词总结
目录 模块安装 开源代码 基本用法 启用Paddle 词性标注 调整词典 智能识别新词 搜索引擎模式分词 使用自定义词典 关键词提取 停用词过滤 模块安装 pip install jieba jieb ...
- 从微信小程序到鸿蒙js开发【13】——list加载更多&回到顶部
鸿蒙入门指南,小白速来!从萌新到高手,怎样快速掌握鸿蒙开发?[课程入口] 目录: 1.list加载更多 2.list回到顶部 3.<从微信小程序到鸿蒙js开发>系列文章合集 1.list加 ...
- 在 2021 年你需要掌握的 7 种关于 JavaScript 的数组方法
在新的一年我们学习这些有用的方法 JavaScript 为我们提供了许多处理数组的不同方法.我们将在几分钟内为您介绍 7 个基本且常用的数据方法,以提高您的 JS 开发技能. 1. Array.map ...
- vscode好用插件总结
做个记录:https://blog.csdn.net/xishining/article/details/90819481 1.Auto Rename Tag --自动重命名成对的HTML标记.假如你 ...