codeforces644B. Processing Queries (模拟)
In this problem you have to simulate the workflow of one-thread server. There are n queries to process, the i-th
will be received at moment ti and
needs to be processed for di units
of time. All ti are
guaranteed to be distinct.
When a query appears server may react in three possible ways:
- If server is free and query queue is empty, then server immediately starts to process this query.
- If server is busy and there are less than b queries in the queue, then new query is added to the end of the queue.
- If server is busy and there are already b queries pending in the queue, then new query is just rejected and will never be processed.
As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment x,
and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears.
For each query find the moment when the server will finish to process it or print -1 if this query will be rejected.
The first line of the input contains two integers n and b (1 ≤ n, b ≤ 200 000) —
the number of queries and the maximum possible size of the query queue.
Then follow n lines with queries descriptions (in chronological order). Each description consists of two integers ti and di (1 ≤ ti, di ≤ 109),
where ti is
the moment of time when the i-th query appears and di is
the time server needs to process it. It is guaranteed that ti - 1 < ti for
all i > 1.
Print the sequence of n integers e1, e2, ..., en,
where ei is
the moment the server will finish to process the i-th query (queries are numbered in the order they appear in the input) or - 1 if
the corresponding query will be rejected.
5 1
2 9
4 8
10 9
15 2
19 1
11 19 -1 21 22
4 1
2 8
4 8
10 9
15 2
10 18 27 -1
Consider the first sample.
- The server will start to process first query at the moment 2 and will finish to process it at the moment 11.
- At the moment 4 second query appears and proceeds to the queue.
- At the moment 10 third query appears. However, the server is still busy with query 1, b = 1 and
there is already query 2 pending in the queue, so third query is just rejected. - At the moment 11 server will finish to process first query and will take the second query from the queue.
- At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue.
- At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the
statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. - Server finishes to process query number 4 at the moment 21.
Query number 5 is picked from the queue. - Server finishes to process query number 5 at the moment 22.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 200050
ll t[maxn],d[maxn];
int q[511111];
ll ans[maxn];
int main()
{
int i,j,x;
ll n,m,b,ed;
while(scanf("%lld%lld",&n,&b)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%lld%lld",&t[i],&d[i]);
}
int front,rear;
front=1;rear=0;
ed=0;
for(i=1;i<=n;i++){
while(front<=rear){
x=q[front];
if(ed<=t[i]){
ed=max(ed,t[x])+d[x];
ans[x]=ed;
front++;
}
else break;
}
if(ed<=t[i] && front>rear ){
ed=t[i]+d[i];
ans[i]=ed;
}
else if(rear-front+1<b){
rear++;
q[rear]=i;
}
else ans[i]=-1;
}
while(front<=rear){
x=q[front];
front++;
ed=max(ed,t[x])+d[x];
ans[x]=ed;
}
for(i=1;i<=n;i++){
if(i==n)printf("%lld\n",ans[i]);
else printf("%lld ",ans[i]);
}
}
return 0;
}
codeforces644B. Processing Queries (模拟)的更多相关文章
- CROC 2016 - Qualification B. Processing Queries 模拟
B. Processing Queries 题目连接: http://www.codeforces.com/contest/644/problem/B Description In this prob ...
- Code Forces 644B Processing Queries
B. Processing Queries time limit per test5 seconds memory limit per test256 megabytes inputstandard ...
- Codeforces Round #515 (Div. 3) C. Books Queries (模拟)
题意:有一个一维的书架,\(L\)表示在最左端放一本书,\(R\)表示在最右端放一本书,\(?\)表示从左数或从右数,最少数多少次才能得到要找的书. 题解:我们开一个稍微大一点的数组,从它的中间开始模 ...
- 代码本色 用编程模拟自然系统 (Daniel Shiffman 著)
https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.7/p5.js http://www.box2d.org http://www.jbox2d.org ...
- Programming Entity Framework 翻译(1)-目录
1. Introducing the ADO.NET Entity Framework ado.net entity framework 介绍 1 The Entity Relationship Mo ...
- 【mysql】关于临时表
mysql官方的介绍 In some cases, the server creates internal temporary tables while processing queries. Suc ...
- open_table与opened_table
好多人在调优Mysql的时候,总是对open_tables和opend_tables两个参数分别不清. 网上好多解释都是这样的:open_tables:当前打开表的数量opened_tables:当前 ...
- 【MySQL】查询使用临时表
MySQL查询产生临时表的分析 官网说明的地址:http://dev.mysql.com/doc/refman/5.5/en/internal-temporary-tables.html 参考:htt ...
- mysql --The MEMORY Storage Engine--官方文档
原文地址:http://dev.mysql.com/doc/refman/5.7/en/memory-storage-engine.html The MEMORY storage engine (fo ...
随机推荐
- 【SpringBoot1.x】 Docker
SpringBoot1.x Docker 核心概念 Docker 是一个开源的应用容器引擎,是一个轻量级容器技术.Docker 支持将软件编译成一个镜像,然后在镜像中各种软件做好配置,将镜像发布出去, ...
- Nginx基础知识学习(安装/进程模型/事件处理机制/详细配置/定时切割日志)
一.Linux下Nginx的安装 1.去官网 http://nginx.org/download/下载对应的Nginx安装包,推荐使用稳定版本. 2.上传Nginx到Linux服务器. 3.安装依赖环 ...
- 四:WEB源码扩展
前言:WEB源码在安全测试中是非常重要的信息来源,可以用来进行代码审计漏洞也可以用来做信息突破口,其中WEB源码有很多技术需要简明分析,获取某ASP源码后就可以采用默认数据库下载为突破,获取某其他脚本 ...
- 容器编排系统K8s之Prometheus监控系统+Grafana部署
前文我们聊到了k8s的apiservice资源结合自定义apiserver扩展原生apiserver功能的相关话题,回顾请参考:https://www.cnblogs.com/qiuhom-1874/ ...
- Spring MVC 接收 LocalDate、LocalTime 和 LocalDateTime Java 8 时间类型参数
使用 Spring MVC 时,很多业务场景下 Controller 需要接收日期时间参数.一个简单的做法是使用 String 接收日期时间字符串(例如:2020-01-29),然后在代码中将其转换成 ...
- 【EXPDP】expdp/impdp数据泵远程导入导出
Oracle在10g的时候有一个很好用的导出工具expdp(数据泵) 但是这个工具好用的同时,有一个局限,就是必须用本地的用户才可以导出数据,也就是说数据泵只能导出本地数据库的数据 但是如果业务需求是 ...
- 【Oracle】Oracle 10g下载路径
ORACLE 10g下载地址 下载方法: 直接复制下面的链接,打开迅雷,自动会识别下载的内容 Oracle Database 10g Release 2 (10.2.0.1.0) Enterprise ...
- ios获取缓存文件的大小并清除缓存
移动应用在处理网络资源时,一般都会做离线缓存处理,其中以图片缓存最为典型,其中很流行的离线缓存框架为SDWebImage. 但是,离线缓存会占用手机存储空间,所以缓存清理功能基本成为资讯.购物.阅读类 ...
- 1.2V升3.3V芯片,大电流,应用MCU供电,3.3V稳压源
MCU供电一般是2.5V-5V之间等等都有,1.2V需要升到3.3V的升压芯片来稳压输出3.3V给MCU供电. 同时1.2V的输入电压低,说明供电端的能量也是属于低能量的,对于芯片自身供货是也要求高. ...
- 向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾。
1 import java.io.FileInputStream; 2 import java.io.IOException; 3 import java.text.SimpleDateFormat; ...