[POJ2828] Buy Tickets(待续)
[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(待续)的更多相关文章
- 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 ...
- poj-2828 Buy Tickets(线段树,排队问题,逆向思维)
题目地址:POJ 2828 Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Yea ...
- [poj2828] Buy Tickets (线段树)
线段树 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must ...
随机推荐
- K8S(14)监控实战-grafana出图_alert告警
k8s监控实战-grafana出图_alert告警 目录 k8s监控实战-grafana出图_alert告警 1 使用炫酷的grafana出图 1.1 部署grafana 1.1.1 准备镜像 1.1 ...
- Shell 函数 & 数组
Shell 函数 函数介绍 # 什么是函数? 具备某一功能的工具 => 函数 事先准备工具的过程 => 函数的定义 遇到应用场景拿来就用 => 函数的调用 # 为何要用函数? 没有引 ...
- kubernetes实战-配置中心(三)配置服务使用apollo配置中心
使用配置中心,需要开发对代码进行调整,将一些配置,通过变量的形式配置到apollo中,服务通过配置中心来获取具体的配置 在配置中心修改新增如下配置: 项目信息: 配置: 重新打包镜像,使用apollo ...
- C# 数据类型(2)
String char的集合 string name = "John Doe"; 双引号,char是单引号string是不可变的,一旦初始化后就不能变了,每次对已存在的string ...
- Hive Tutorial 阅读记录
Hive Tutorial 目录 Hive Tutorial 1.Concepts 1.1.What Is Hive 1.2.What Hive Is NOT 1.3.Getting Started ...
- yarn create & npx & npm init
yarn create & npx & npm init https://www.npmtrends.com/npm-vs-npx-vs-yarn demo https://www.n ...
- window resize & resize observer
window resize & resize observer https://developer.mozilla.org/en-US/docs/Web/API/Window/resize_e ...
- 以代码为剑、数学为犁,SPC构建NGK算力生态体系
人类创造工具,工具反过来也改变着人类.以区块链为核心的货币革命率先吹响了对金融世界重塑的号角.以代码为剑.数学为犁,区块链构建了新的网路信任体系,这是一切的开始.基于此,NGK区块链技术将赋能实体产业 ...
- Java基础篇(04):日期与时间API用法详解
本文源码:GitHub·点这里 || GitEE·点这里 一.时间和日期 在系统开发中,日期与时间作为重要的业务因素,起到十分关键的作用,例如同一个时间节点下的数据生成,基于时间范围的各种数据统计和分 ...
- WebRTC 系列之音频会话管理
WebRTC(Web Real-Time Communication)是一个支持网页浏览器进行实时语音对话或视频对话的 API.W3C 和 IETF 在2021年1月26日共同宣布 WebRTC 1. ...