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,如果有比他年轻的大象排在他前面,这头大象就会非常 ...
随机推荐
- RabbitMQ学习之:(十)AMQP和RabbitMQ介绍 (转贴+我的评论)
From: http://www.infoq.com/cn/articles/AMQP-RabbitMQ 准备开始 高级消息队列协议(AMQP1)是一个异步消息传递所使用的应用层协议规范.作为线路层协 ...
- java配置SSM框架下的redis缓存
pom.xml引入依赖包 <!--jedis.jar --> <dependency> <groupId>redis.clients</groupId> ...
- 关于java面试题
java的优点: Java是一种跨平台,适合于分布式计算环境的面向对象编程语言. 具体来说,它具有如下特性: 简单性.面向对象.分布式.解释型.可靠.安全.平台无关.可移植.高性能.多线程.动态性等.
- 分布式存储ceph介绍(1)
一.Ceph简介: Ceph是一种为优秀的性能.可靠性和可扩展性而设计的统一的.分布式文件系统.ceph 的统一体现在可以提供文件系统.块存储和对象存储,分布式体现在可以动态扩展.在国内一些公司的云环 ...
- Redis4.0新特性 -Lazy Free
Redis4.0新增了非常实用的lazy free特性,从根本上解决Big Key(主要指定元素较多集合类型Key)删除的风险.笔者在redis运维中也遇过几次Big Key删除带来可用性和性能故障. ...
- android#全局获取Context的技巧
参考<第一行代码>——郭霖 回想这么久以来我们所学的内容,你会发现有很多地方都需要用到Context,弹出Toast的时候需要.启动活动的时候需要.发送广播的时候需要.操作数据库的时候需要 ...
- windows部署服务(WDS)
1.服务器端os:windows server2003R2,windows server 2008,windows server 2008 R2 文件系统:NTFS 必须需要AD架构 网络中需要微软 ...
- 【ARM-Linux开发】Linux的SOCKET编程详解
Linux的SOCKET编程详解 1. 网络中进程之间如何通信 进 程通信的概念最初来源于单机系统.由于每个进程都在自己的地址范围内运行,为保证两个相互通信的进 程之间既互不干扰又协调一致工作,操作系 ...
- 关于socket buffer size的调优
为了达到最大网络吞吐,socket send buffer size(SO_SNDBUF)不应该小于带宽和延迟的乘积.之前我遇到2个性能问题,都和SO_SNDBUF设置得太小有关.但是,写程序的时候可 ...
- activeMQ(2)
queue与topic的对比 JMS MESSAGE:消息头 消息体 消息属性 DeliveryMode: 消息体: 消息属性:识别 去重 重点标注 //创建会话session 事务.签收 如果 ...