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:

  1. If server is free and query queue is empty, then server immediately starts to process this query.
  2. 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.
  3. 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
Note

Consider the first sample.

  1. The server will start to process first query at the moment 2 and will finish to process it at the moment 11.
  2. At the moment 4 second query appears and proceeds to the queue.
  3. 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.
  4. At the moment 11 server will finish to process first query and will take the second query from the queue.
  5. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue.
  6. 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.
  7. Server finishes to process query number 4 at the moment 21.
    Query number 5 is picked from the queue.
  8. Server finishes to process query number 5 at the moment 22.
题意:有n个任务要处理,每一次只能处理一个,待处理的任务放在任务队列中,每次一个任务来的时候,队列里的任务都处理完了,那么就开始处理这个任务;如果队列里的任务没有处理完,而且队列中的任务数量小于b,那么处理把这个任务加入队列;否则把这个任务舍去,问每一个任务执行结束后的时间。

思路:开一个队列,用来模拟任务队列。每次一个任务到来的时候,先把这个任务到来前的能处理的任务都处理完,然后看队列是不是为空并且处理完所有任务的时间小于等于这个任务到来的时间(即判断能不能直接处理这个任务),如果不能直接处理这个任务的话,判断任务队列里的任务个数是不是等于b,如果是就舍去这个任务,否则把这个任务放入队列中。

#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; }


最新文章

  1. 高性能MySQL(四):schema陷阱
  2. angularjs指令参数transclude
  3. jquery ajax beforeSend 提交等待问题
  4. tyvj1194 划分大理石
  5. 使用Flexible适配移动端html页面 - demo记录
  6. Hihocoder 1063 缩地
  7. 绘制 ToggleButton --重写view
  8. ubuntu-12.10-server中打开终端的方式
  9. Range-Based for Loops
  10. python 三元运算符
  11. vsftpd2.3.2安装、配置详解
  12. 重构14-Break&#160;Responsibilities
  13. [C++]Standing Ovation——Google Code Jam 2015 Qualification Round
  14. java--线程状态
  15. struts2由&amp;lt;s:tree&amp;gt;要么dtree小工具 建立树
  16. Java 集合 JDK1.7的LinkedList
  17. DWR 整合之Hibernate
  18. php中urldecode()和urlencode()起什么作用
  19. kaggle learn python
  20. debian 安装libreoffice6.1 转换pdf

热门文章

  1. JavaScript 获取当天0点以及当前时间方法
  2. python中re模块的使用(正则表达式)
  3. Hbase RIT故障修复
  4. 【MySQL】ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing
  5. 【Linux】大于2T的磁盘怎么分区?
  6. bootstrap弹出层嵌套弹出层后文本框不能获得焦点输入
  7. 特斯拉Toolbox诊断检测仪工具Tesla诊断电脑 Tesla Toolbox
  8. Android 8.0/9.0 wifi 自动连接评分机制
  9. Connection Manager简称connman
  10. Base64原理 bits 3-&gt;4 8bits/byte--&gt;6bits/byte