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 ...
随机推荐
- [k8s]metricbeat的kubernetes模块&kube-metric模块
正确姿势启动metricbeat metricbeat.modules: - module: system metricsets: - cpu - filesystem - memory - netw ...
- 2016 年 Java 优秀文章
https://www.ibm.com/developerworks/cn/java/j-2016-java-good-article/index.html
- 基于jQuery select下拉框美化插件
分享一款基于jQuery select下拉框美化插件.该插件适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗.效果图如下: 在线预览 源码下 ...
- 一些lua代码
1.把k--v表转化为数组表,只支持2级 2.取中值 3.字符串按每行最多n像素分割,并返回每行最大宽度,可以用"\n"手动换行
- 未能加载文件或程序集“Autofac, Version=3.4.0.0,
遇到这个错误的时候:如下图 未能加载文件或程序集“Autofac, Version=3.4.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da” ...
- https 单向双向认证说明_数字证书, 数字签名, SSL(TLS) , SASL_转
转自:https 单向双向认证说明_数字证书, 数字签名, SSL(TLS) , SASL 因为项目中要用到TLS + SASL 来做安全认证层. 所以看了一些网上的资料, 这里做一个总结. 1. 首 ...
- NAT--Network Address Translator
定义 Nat用于在本地网络中使用私有地址,在连接互联网时转而使用全局IP地址的技术.除了转换IP地址外,还出现了可以转换TCP.UDP端口号的NAPT(Network Address Ports Tr ...
- API - 使用数据仓库 - 基础篇
数据仓库是集成在Spider Studio中的一个重要功能, 利用它可以非常方便的保存采集到的数据, 然后导出或者在其他脚本中再利用. 数据仓库的全部功能都集成在DataManager这个静态类里面, ...
- 2015 Multi-University Training Contest 3 1002 RGCDQ
RGCDQ Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5317 Mean: 定义函数f(x)表示:x的不同素因子个数. 如:f ...
- 【转】【Linux】grep命令详解
简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...