Codeforces 1239C. Queue in the Train
事实上就是模拟
搞一个优先队列维护一下事件结构体:时间,人的编号,入队还是出队
再维护两个 $set$ ,队列内的人 $inQueue$ ,想要进入队列内的人 $want$
然后模拟模拟模拟!
初始把所有入队事件塞到优先队列,顺便维护一下当前最后一个取完水的时刻
每次取出优先队列里面时间最小的,时间相同优先取入队的,同时间都入队优先取编号小的
然后如果是入队,那么看看当前队列内编号最小的比较一下编号,然后根据比较结果看看是直接入队还是先塞到 $want$ 里面
如果是出队,直接更新一下答案和队列,然后看看 $want$ 里面有没有人能入队
然后就做完了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=2e5+;
int n,P;
ll ans[N];
struct dat {
ll tim; int id; bool flag;
dat (ll _tim=,int _id=,bool _flag=) { tim=_tim,id=_id,flag=_flag; }
inline bool operator < (const dat &tmp) const {
if(tim!=tmp.tim) return tim>tmp.tim;
return flag!=tmp.flag ? flag>tmp.flag : id>tmp.id;
}
}D[N];
priority_queue <dat> Q;
set <int> inQ,wnt;
int main()
{
n=read(),P=read();
for(int i=;i<=n;i++) D[i]=dat(read(),i,);
for(int i=;i<=n;i++) Q.push(D[i]);
ll las=;
while(!Q.empty())
{
dat t=Q.top(); Q.pop();
if(!t.flag)
{
if(inQ.empty()||*inQ.begin()>t.id)
{
las=max(las,t.tim),Q.push(dat(las+P,t.id,));
inQ.insert(t.id); las+=P;
}
else wnt.insert(t.id);
continue;
}
ans[t.id]=t.tim; inQ.erase(t.id);
if(!wnt.empty() && (inQ.empty()||*inQ.begin()>*wnt.begin()))
{
auto p=wnt.begin(); inQ.insert(*p);
Q.push(dat(las+P,*p,)); las+=P; wnt.erase(p);
}
}
for(int i=;i<=n;i++) printf("%lld ",ans[i]); puts("");
return ;
}
Codeforces 1239C. Queue in the Train的更多相关文章
- Codeforces Round #594 (Div. 1) C. Queue in the Train 模拟
C. Queue in the Train There are
- codeforces D. Queue 找规律+递推
题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...
- Codeforces Beta Round #8 A. Train and Peter KMP
A. Train and Peter 题目连接: http://www.codeforces.com/contest/8/problem/A Description Peter likes to tr ...
- Serega and Fun Codeforces - 455D || queue
https://codeforces.com/problemset/problem/455/D 其实方法很多,然而当初一个也想不到... 1.分块,块内用链表维护 修改[l,r]就当成删除第r个元素, ...
- CodeForces 91B Queue (线段树,区间最值)
http://codeforces.com/problemset/problem/91/B B. Queue time limit per test: 2 seconds memory limit p ...
- codeforces 490B.Queue 解题报告
题目链接:http://codeforces.com/problemset/problem/490/B 题目意思:给出每个人 i 站在他前面的人的编号 ai 和后面的人的编号 bi.注意,排在第一个位 ...
- Codeforces 353D Queue(构造法)
[题目链接] http://codeforces.com/contest/353/problem/D [题目大意] 10^6个男女排队,每一秒,如果男生在女生前面,即pos[i]是男生,pos[i+1 ...
- Codeforces 490B Queue【模拟】
题意还是很好理解的,根据题目给出描述条件然后求出这串QUEUE 我的做法就是用两个数组 before[] 和 after[] 表示 ai 前面的前面的人的学号 和 ai 后面的后面的人的学号 ex[] ...
- CodeForces 91B Queue
题目链接:http://codeforces.com/contest/91/problem/B 题目大意: 有n头大象排队买票,第i头大象的年龄为ai,如果有比他年轻的大象排在他前面,这头大象就会非常 ...
随机推荐
- Go Iris 中间件
Iris 中间件 当我们在 iris 中讨论中间件时,我们讨论的是在HTTP请求生命周期中在主处理程序代码之前和/或之后的运行代码. 实现中间件功能,有下面这样两种方式: 方式一: 我们可以通过按顺序 ...
- 初始html5
一.html5的发展历史: 2004年提出构想:2008年发布第一份草案:2012年前后位推广阶段:2020年最终测试:2022年正式发布. 二.html5新特性: (1)html5 语义化更好: 新 ...
- 黑马vue---19、v-for中key的使用注意事项
黑马vue---19.v-for中key的使用注意事项 一.总结 一句话总结: 必须 在使用 v-for 的同时,指定 唯一的 字符串/数字 类型 :key 值 <p v-for="i ...
- Bootstrap4从入门到精通视频教程
一.布局 0.课件1.Bootstrap介绍_栅格系统2.禁用响应式_响应式分界点 二.内容 3.排版_代码4.图片_图片框5.表格 三.公共样式 6.边框_浮动7.颜色_Display显示属性8.文 ...
- EBR-TLV数据格式
EMV规范中的BER-TLV数据格式:BER-TLV结构由Tag.Length.Value三部分组成. [TAG域]TAG可以由1个与多个字节组成,TAG域的第一个字节编码格式如下: 其中由三部分组成 ...
- vlc的流输出功能
vlc的流输出功能 流输出功能,可以将vlc读取到的流,输出到文件或者通过网络发送,客户端可以使用http.rtp.rtsp等协议访问,还可以进行转码等操作. 参考http://wiki.videol ...
- [CDH] Cloudera's Distribution including Apache Hadoop
You may choose to install spark, yarn, hive, etc one by one. [Spark] 00 - Install Hadoop & Spark ...
- Expecting "jsp:param" standard action with "name" and "value" attributes
浏览器访问报如下jsp标签错误: 根据提示,定位到jsp页面124行,代码如下: 查找原因,当<jsp:include></jsp:include>标签中没有参数时,不允许有空 ...
- sudo: unable to execute ./xxx.py: no such file or directory
$ ./mk_dataset.py -bash: ./mk_dataset.py: /home/ocean1101/anaconda3/bin/python^M: bad interpreter: N ...
- Unity Shader的形式
(1)表面着色器 表面着色器是Unity自身的一种着色器代码类型.它需要的代码量很少,Unity在背后做了很多工作,但渲染的代价比较大.但Unity在背后仍旧把表面着色器转换成对应的顶点/片元着色器. ...