Code Forces 644B Processing Queries
B. Processing Queries
time limit per test5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Input
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.
Output
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.
Examples
input
5 1
2 9
4 8
10 9
15 2
19 1
output
11 19 -1 21 22
input
4 1
2 8
4 8
10 9
15 2
output
10 18 27 -1
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
#define MAX 200000
struct Node
{
int id;
long long int st;
long long int time;
}a[MAX+5];
int cmp(Node a,Node b)
{
return a.st<b.st;
}
queue<Node> q;
long long int ans[MAX+5];
int n,b;
int main()
{
scanf("%d%d",&n,&b);
for(int i=1;i<=n;i++)
{
scanf("%lld%lld",&a[i].st,&a[i].time);
a[i].id=i;
}
sort(a+1,a+n+1,cmp);
int i=2;
long long int now=a[1].st+a[1].time;
ans[1]=now;
while(1)
{
if(i>n&&q.size()==0)
break;
while(i<=n)
{
if(a[i].st>=now)
break;
if(q.size()>=b)
ans[a[i++].id]=-1;
else
{
q.push(a[i++]);
}
}
if(q.size()==0)
{now=a[i].st+a[i].time;ans[i]=now;i++;continue;}
Node term=q.front();
q.pop();
now+=term.time;
ans[term.id]=now;
}
for(int i=1;i<=n;i++)
{
if(i==n)
printf("%lld\n",ans[i]);
else
printf("%lld ",ans[i]);
}
return 0;
}
Code Forces 644B Processing Queries的更多相关文章
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- CROC 2016 - Qualification B. Processing Queries 模拟
B. Processing Queries 题目连接: http://www.codeforces.com/contest/644/problem/B Description In this prob ...
- Spark SQL includes a cost-based optimizer, columnar storage and code generation to make queries fast.
https://spark.apache.org/sql/ Performance & Scalability Spark SQL includes a cost-based optimize ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- Code Forces 833 A The Meaningless Game(思维,数学)
Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...
- Code Forces 543A Writing Code
题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...
- codeforces644B. Processing Queries (模拟)
In this problem you have to simulate the workflow of one-thread server. There are n queries to proce ...
- code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)
Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...
- code forces 382 D Taxes(数论--哥德巴赫猜想)
Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
随机推荐
- 让gcc和gdb支持intel格式的汇编
Linux下的gdb和gcc默认输出的汇编都是AT&T格式的,但是它们都有方式来转换为Intel格式. -masm=[intel|att] 选择intel或AT&T的汇编语法 gcc ...
- Spring Boot与Spring Security整合后post数据不了,403拒绝访问
http://blog.csdn.net/sinat_28454173/article/details/52251004 *************************************** ...
- bootstrap.memory_lock: true导致Elasticsearch启动失败问题
elasticsearch官网建议生产环境需要设置bootstrap.memory_lock: true 重新启动elasticsearch,报错信息如下: [baoshan@test-43.dev. ...
- oracle 命令 登录
1.打开终端,输入命令:sqlplus /nolog 2.连接数据库:conn user_name/passwd@db_name
- Unix系统编程()文件控制操作fcntl
fcntl系统调用对一个打开的文件描述符执行一系列的控制操作. int fcntl(int fd, int cmd, -) cmd参数所支持的操作范围很广 fcntl的第三个参数以省略号表示,意味着可 ...
- linux 域和xenomai 实时域之间的交互
/* * XDDP-based RT/NRT threads communication demo. * * Real-time Xenomai threads and regular Linux t ...
- js 拼接字符串 穿参数 带有单引号
var html="<a href=\"#\" onclick=Unlock(\""+flid+"\",1)>弹出& ...
- 如何从CentOS官网下载我们想要的版本
今天想从官网下载6.5版本的CentOS,结果找了好一会儿才找到,赶紧记录下来,以备以后查询. 第一步在百度搜索centos,点击"Download CentOS",如下图所示. ...
- Bitcoin: A Peer-to-Peer Electronic Cash System(比特币论文翻译)
比特币历史: 2008年,比特币论文诞生 2009年1月,第一批比特币诞生 2011年4月,比特币价格第一次达到了1美元 2011年6月,涨到30美元,然后开始跌 2013年1月,4美元 2013年1 ...
- linux -- Ubuntu network-manager
[Server版本] 在UbuntuServer版本中,因为只存有命令行模式,所以要想进行网络参数设置,只能通过修改/etc/network/interfaces.具体设置方法如下: (1) Ubun ...