[POJ2828] Buy Tickets(待续)

题目大意:多组测试,每组给出\(n\)条信息\((a,b)\),表示\(b\)前面有\(a\)个人,顺序靠后的信息优先级高

Solution.1

由后向前看,每个遇到的都是确定位置的,最后的人选定的位置不会改变,同样因为是倒叙输入,在第\(i\)个人后插队,也就是说他的前面一定要留下\(i\)个空格。

形象一点就是这样:

从后往前,去查找第一个大于所需要空白的位置。用线段树维护空格数目即可

Code.1

#include <iostream>
#include <cstdio>
#include <algorithm> const int N = 2e5 + 10; int n;
int a[N], b[N];
int num[N << 2], spa[N << 2]; inline void pushup(int cur){
spa[cur] = spa[cur << 1] + spa[cur << 1 | 1];
return;
} void build(int cur, int l, int r){
int mid = l + ((r - l) >> 1);
if(l == r){
spa[cur] = 1;
num[cur] = 0;
}else{
build(cur << 1, l, mid);
build(cur << 1 | 1, mid + 1, r);
pushup(cur);
}
} void update(int cur, int l, int r, int la, int lb){
if(l == r){
spa[cur] = 0;
num[cur] = lb;
}else{
int mid = l + ((r - l) >> 1); if(spa[cur << 1] >= la){
update(cur << 1, l, mid, la, lb);
}else{
update(cur << 1 | 1, mid + 1, r, la - spa[cur << 1], lb);
}
pushup(cur);
}
} inline void print(int cur, int l, int r){
int mid = l + ((r - l) >> 1);
if(l == r){
printf("%d ", num[cur]);
return;
}else{
print(cur << 1, l, mid); print(cur << 1 | 1, mid + 1, r);
}
return;
} int main(){ while(scanf("%d", &n) != EOF){
build(1, 1, n);
for(int i = 1; i <= n; ++i){
scanf("%d %d", &a[i], &b[i]);
a[i] ++;
}
for(int i = n; i; --i){
update(1, 1, n, a[i], b[i]);
}
print(1, 1, n);
printf("\n");
} return 0;
}

Solution.2

用splay优雅接上

Code.2

[POJ2828] Buy Tickets(待续)的更多相关文章

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

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

  2. poj-----(2828)Buy Tickets(线段树单点更新)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12930   Accepted: 6412 Desc ...

  3. POJ2828 Buy Tickets 【线段树】+【单点更新】+【逆序】

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12296   Accepted: 6071 Desc ...

  4. poj2828 Buy Tickets (线段树 插队问题)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 22097   Accepted: 10834 Des ...

  5. poj-2828 Buy Tickets(经典线段树)

    /* Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 10207 Accepted: 4919 Descr ...

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

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

  7. POJ2828 Buy Tickets 树状数组

    Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...

  8. poj-2828 Buy Tickets(线段树,排队问题,逆向思维)

    题目地址:POJ 2828 Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Yea ...

  9. [poj2828] Buy Tickets (线段树)

    线段树 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must ...

随机推荐

  1. ++i和i++的区别

    它们两个的数值变化的区别,我这里就不多说了 这里主要说明两者在效率上的区别 (1)首先如果是自带的数据类型,比如int型,++i和i++,编译器的实现方式是相同的,两者并没有效率上的区别,虽然也有副本 ...

  2. Java之先行发生原则与volatile关键字详解

    volatile关键字可以说是Java虚拟机提供的最轻量级的同步机制,但是它并不容易完全被正确.完整地理解,以至于许多程序员都习惯不去使用它,遇到需要处理多线程数据竞争问题的时候一律使用synchro ...

  3. HDU 4335 What is N?(指数循环节)题解

    题意: 询问有多少数\(n\)满足\(n^{n!}\equiv b\mod p \land\ n\in[1,M]\),数据范围:\(M\leq2^{64}-1,p\leq1e5\) 思路: 这题显然要 ...

  4. Hexo-域名设置+收录

    Hexo-域名设置+Github域名加速+网址收录 Github.Gitee绑定域名,然后进行网址收录. 不想购买域名也完全可以进行网址收录. 购买阿里云域名 1.进入阿里云域名网站 2.进入价格详情 ...

  5. ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛

    #1235 : New Teaching Buildings 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 Thanks to the generous finance ...

  6. Linux shell script All In One

    Linux shell script All In One refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  7. Koa & WebSocket inAction

    Koa & WebSocket inAction node.js https://koajs.com/ ping / pong socket.io client send 2 as ping ...

  8. js 垃圾回收 & js GC

    js 垃圾回收 & js GC js GC / js Garbage Collector https://developer.mozilla.org/en-US/docs/Web/JavaSc ...

  9. Linux & bash & tcpdump

    Linux & bash & tcpdump Linux & tcpdump https://www.tecmint.com/12-tcpdump-commands-a-net ...

  10. disable html input & pointer-events

    disable html input & pointer-events css https://developer.mozilla.org/en-US/docs/Web/CSS/pointer ...