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,如果有比他年轻的大象排在他前面,这头大象就会非常 ...
随机推荐
- mac安装genymotion遇到的问题记录
1.出错内容:An error occured while deploying the file或者使用adb devices连接的时候出现下面的错误 adb server version (40) ...
- shell编程之 ()[] {}
shell脚本中各种括号的区别以及用法 2018年08月19日 14:55:33 M_QiJunChao 阅读数:273 最近学到了shell脚本编程,觉得脚本中的不同括号有不同的用处,以及有些括 ...
- 手把手教你设置MongoDB密码
mongodb密码和传统数据如mysql等有些区别: mongodb的用户名和密码是基于特定数据库的,而不是基于整个系统的.所有所有数据库db都需要设置密码. 1. 查看所有数据库(在mongodb新 ...
- Fiddler主界面图标简单说明
Fiddler主界面图标简单说明: 名称 含义 # 抓取HTTP Request的顺序,从1开始,以此递增 Result HTTP状态码 Protocol 请求使用的协议,如HTTP/HTTPS/FT ...
- P4104 [HEOI2014]平衡
友情提醒:取模太多真的会TLE!!! P4104 [HEOI2014]平衡 题解 本题属于 DP-整数划分 类问题中的 把整数 n 划分成 k 个不相同不大于 m 的正整数问题 设置DP状态 f[ ...
- flutter richText富文本
flutter中富文本使用 RichText( text: TextSpan( text: '登陆即同意', style: TextStyle(fontSize: 14, color: Colors. ...
- selenium 等待时间
三种时间模式:1.隐性等待:①等待页面所有元素都加载完才执行下一步,如果在设定的时间内没有加载完成所有元素,则抛出异常②隐式等待对整个driver周期都起作用,即设置一次后,所有执行都会有效from ...
- JAVA 基础编程练习题6 【程序 6 求最大公约数及最小公倍数】
6 [程序 6 求最大公约数及最小公倍数] 题目:输入两个正整数 m 和 n,求其最大公约数和最小公倍数. 程序分析:利用辗除法. package cskaoyan; public class csk ...
- React Native解决安卓图片被挤压
Bug如下图所示: iOS显示正常,而安卓出现图片被挤压上去. 最后的解决方法: 比如你的 图片 是 750 x 513 那么 你设置样式的时候 width 为 屏幕的宽 ,高度为 屏幕的 宽 / ( ...
- socket --自己简单的理解
一,网络编程中两个主要的问题 一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后如何可靠高效的进行数据传输. 在TCP/IP协议中IP层主要负责网络主机的定位,数据传输的路由,由IP地址可 ...