[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. 导出Excel出错

    错误提示:   解决方法: 1.运行dcomcnfg打开组件服务. 2.依次展开"组件服务"->"计算机"->"我的电脑"-&g ...

  2. C# wpf window

    使用vs2017 新建wpf 项目 MainWindow 被定义为partial,是因为他要和xaml的一些属性组合在一起,然后再运行起来,这正是 InitailizeCompoent 这个函数要干的 ...

  3. 洛谷p1966 火柴排队 (逆序对变形,目标排序

    题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为: ∑(ai-bi)^2 其中 ai 表示 ...

  4. LWIP再探----内存堆管理

    LWIP的内存管理主要三种:内存池Pool,内存堆,和C库方式.三种方式中C库因为是直接从系统堆中分配内存空间且易产生碎片因此,基本不会使用,其他两种是LWIP默认全部采用的方式,也是综合效率和空间的 ...

  5. 如何在 VSCODE 中高效使用 R 语言

    VSCODE 配置 R 一.功能特性展示 之前一直在用 Rstudio 来编写 R,也尝试用过 Pycharm 配置 R 环境. 但是由于现在需求要同时满足 Python,R 和网站要同时开发,为了避 ...

  6. TypedArray & ArrayBuffer

    TypedArray & ArrayBuffer Type Each element size in bytes Int8Array 1 Uint8Array 1 Uint8ClampedAr ...

  7. Web Performance API

    Web Performance API 性能监测/性能优化 https://developer.mozilla.org/en-US/docs/Web/API/Performance https://d ...

  8. Python Quiz & Python Exercise

    Python Quiz & Python Exercise https://www.w3schools.com/quiztest/quiztest.asp?qtest=PYTHON https ...

  9. react-parent-child-lifecycle-order

    react-parent-child-lifecycle-order react parent child lifecycle order live demo https://33qrr.csb.ap ...

  10. stackoverflow & xgqfrms

    stackoverflow & xgqfrms stackoverflow https://stackoverflow.com/users/5934465/xgqfrms https://st ...