Atcoder 212F Greedy Takahashi
Problem Statement
There are N cities numbered 1 through N, and MM buses that go between these cities. The i-th bus (1≤i≤M) departs from City Ai at time Si+0.5 and arrive at City Bi at time Ti+0.5.
Takahashi will travel between these N cities. When he is in City p at time t, he will do the following.
- If there is a bus that departs from City p not earlier than time t, take the bus that departs the earliest among those buses to get to another city.
- If there is no such bus, do nothing and stay in City p.
Takahashi repeats doing the above until there is no bus to take. It is guaranteed that all M buses depart at different times, so the bus to take is always uniquely determined. Additionally, the time needed to change buses is negligible.
Here is your task: process Q queries, the i-th (1≤i≤Q)of which is as follows.
- If Takahashi begins his travel in City Yi at time Xi, in which city or on which bus will he be at time Zi?
Constraints
- 2≤N≤105
- 1≤M≤105
- 1≤Q≤105
- 1≤Ai,Bi≤N (1≤i≤M)
- Ai≠Bi (1≤i≤M)
- 1≤Si<Ti≤109 (1≤i≤M)
- Si≠Sj (i≠j)
- 1≤Xi<Zi≤109 (1≤i≤Q)
- 1≤Yi≤N (1≤i≤Q)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M Q
A1 B1 S1 T1
A2 B2 S2 T2
⋮⋮
AM BM SM TM
X1 Y1 Z1
X2 Y2 Z2
⋮⋮
XQ YQ ZQ
Output
Print Q lines. The i-th line should contain the response to the i-th query as follows.
- If Takashi is on some bus at time Zi, print two integers representing the city from which the bus departs and the city at which the bus arrives, in this order, with a space between them.
- Otherwise, that is, if Takahashi is in some city at time Zi, print an integer representing that city.
Sample Input 1 Copy
3 2 3
1 2 1 3
2 3 3 5
1 1 5
2 2 3
1 3 2
Sample Output 1 Copy
2 3
2
3
In the first query, Takahashi will travel as follows.
- Start in City 1 at time 1.
- Take the bus that departs from City 1 at time 1.5 and arrives at City 2 at time 3.5
- Take the bus that departs from City 2 at time 3.5 and arrives at City 3 at time 5.5
- Since no bus departs from City 3 at time 5.5 or later, stay in City 3 (forever).
At time 5, he will be on the bus that departs from City 2 and arrives at City 3. Thus, as specified in the Output section, we should print 2 and 3 with a space between them.
Sample Input 2 Copy
8 10 10
4 3 329982133 872113932
6 8 101082040 756263297
4 7 515073851 793074419
8 7 899017043 941751547
5 7 295510441 597348810
7 2 688716395 890599546
6 1 414221915 748470452
6 4 810915860 904512496
3 1 497469654 973509612
4 1 307142272 872178157
374358788 4 509276232
243448834 6 585993193
156350864 4 682491610
131643541 8 836902943
152874385 6 495945159
382276121 1 481368090
552433623 2 884584430
580376205 2 639442239
108790644 7 879874292
883275610 1 994982498
Sample Output 2 Copy
4
6 1
4 1
8
6 1
1
2
2
7 2
1
题目翻译
\(n\) 个城市,\(m\) 辆车,对于第 \(i\) 辆车在 \(s_i+0.5\) 时刻从 \(u_i\) 出发,于 \(t_i+0.5\) 抵达 \(v_i\)。
在一个点时会选择还没出发且离当前出发时刻最近的车坐上,直到没有车。给出 \(q\) 个询问,问最后会到哪里。
如果在车上,则输出上一个城市和车编号。在城市则输出城市号
题目解析
考虑询问数\(Q<=10^5\),猜测单次查询的复杂度为\(O(logn)\)
倍增,预处理\(f[i][j]\)表示乘坐\(i\)号车,换乘\(2^j\)后所在的车
使用STL的set的lower_bound,通过\(T_i\)求出\(f[i][0]\)
询问时枚举\(j\),一直换乘到无法换乘或时间已到
通过结束时间\(Z\)与当前车次\(T_{now}\)判断在车上还是城市
总结一下此题的要点:
然在每个点要走的都是和时间相关的,但对于一个时间之后,上车下车的走发是唯一的,所以可以把车作为状态。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
int n,m,q;
int u[100005],v[100005],s[100005],t[100005];
int f[100005][20];
set<pair<int, int> >g[100005];
int main(){
int x,y,z;
cin>>n>>m>>q;
for (int i=1;i<=m;i++){
scanf("%d%d%d%d",&u[i],&v[i],&s[i],&t[i]);
g[u[i]].insert(make_pair(s[i],i));
}
for (int i=1;i<=m;i++){
set<pair<int, int> >::iterator it;
it=g[v[i]].lower_bound(make_pair(t[i],0));
if (it!=g[v[i]].end())
f[i][0]=it->second;
}
for (int i=1;i<=17;i++){
for (int j=1;j<=m;j++){
f[j][i]=f[f[j][i-1]][i-1];
}
}
while (q--){
scanf("%d%d%d",&x,&y,&z);
set<pair<int, int> >::iterator it;
it=g[y].lower_bound(make_pair(x,0));
if (it==g[y].end()||z<=s[it->second]){
printf("%d\n",y);
}else{
int now=it->second;
for (int i=17;i>=0;i--){
if (f[now][i]&&s[f[now][i]]<z) now=f[now][i];
}
if (t[now]<z) printf("%d\n",v[now]);
else printf("%d %d\n",u[now],v[now]);
}
}
}
原文地址:https://www.cnblogs.com/Y-E-T-I/p/15135111.html
- 微信小程序价值思考:手机端的CS-BS迁移
- Linq2Sql数据实体外部更新时“不能添加其键已在使用中的实体”的解决办法
- 微信小程序:字体保持大小
- 在为知笔记中使用JQuery
- 在为知笔记中使用JQuery
- Python学习笔记(1):列表元组结构
- 6步创建一个通用机器学习模板
- Python学习笔记(2):数据库访问
- 学习Python语言,这些酷毙的工具你知道几个?
- Mysql备份系列(4)--lvm-snapshot备份mysql数据(全量+增量)操作记录
- 免费体验国内首款写作机器人,就是这么溜!
- Python学习笔记(3):数据集操作-列的统一操作
- Python学习笔记(3):数据集操作-列的统一操作
- Python学习笔记(4):自定义时间类
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Lvs+Keepalived 保障HA高可用
- 基于前端JS导出Excel文件(减轻服务端压力)
- 本机IDEA远程调试远端服务器代码
- 开源 - Java接口API授权认证与规范
- 亿及流量多级缓存 - 客户端缓存
- 亿及流量多级缓存 - 一致性哈希负载均衡与模板渲染
- 关于友情链接或者其他外部链接的建议
- 总结Js方法工具类库,总有你需要的方法
- [docker]安装Mysql
- [Centos7]linux运行django项目报错no module named _ssl
- [Centos7]在非标准端口上运行SSH
- [Centos7]安装及配置bind(DNS服务)
- [Centos7.2]关于crontab报错
- [Centos7.2]关于升级python后防火墙无法启动
- [Centos7]关于限制IP通过ssh登陆