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


codeforces644B. Processing Queries (模拟)的更多相关文章

  1. CROC 2016 - Qualification B. Processing Queries 模拟

    B. Processing Queries 题目连接: http://www.codeforces.com/contest/644/problem/B Description In this prob ...

  2. Code Forces 644B Processing Queries

    B. Processing Queries time limit per test5 seconds memory limit per test256 megabytes inputstandard ...

  3. Codeforces Round #515 (Div. 3) C. Books Queries (模拟)

    题意:有一个一维的书架,\(L\)表示在最左端放一本书,\(R\)表示在最右端放一本书,\(?\)表示从左数或从右数,最少数多少次才能得到要找的书. 题解:我们开一个稍微大一点的数组,从它的中间开始模 ...

  4. 代码本色 用编程模拟自然系统 (Daniel Shiffman 著)

    https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.7/p5.js http://www.box2d.org http://www.jbox2d.org ...

  5. Programming Entity Framework 翻译(1)-目录

    1. Introducing the ADO.NET Entity Framework ado.net entity framework 介绍 1 The Entity Relationship Mo ...

  6. 【mysql】关于临时表

    mysql官方的介绍 In some cases, the server creates internal temporary tables while processing queries. Suc ...

  7. open_table与opened_table

    好多人在调优Mysql的时候,总是对open_tables和opend_tables两个参数分别不清. 网上好多解释都是这样的:open_tables:当前打开表的数量opened_tables:当前 ...

  8. 【MySQL】查询使用临时表

    MySQL查询产生临时表的分析 官网说明的地址:http://dev.mysql.com/doc/refman/5.5/en/internal-temporary-tables.html 参考:htt ...

  9. mysql --The MEMORY Storage Engine--官方文档

    原文地址:http://dev.mysql.com/doc/refman/5.7/en/memory-storage-engine.html The MEMORY storage engine (fo ...

随机推荐

  1. Java JDBC的 url 配置信息和Mybatis核心配置文件(MySQL 的配置信息)

    JDBC 连接数据库的 url driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/smbms?uesSSL=true&u ...

  2. 更改mysql的密码

    mysql> set password for 'root'@'localhost' =PASSWORD('');Query OK, 0 rows affected (0.17 sec) mys ...

  3. 【Oracle LISTNER】oracle Listener 宕机解决办法

    今天想起了很久没用的oracle库,用plsql尝试连接,发现报超时错误,以为是偶然,多次尝试连接,发现还是超时,于是登录到系统中,查看数据库情况,发现正常查询和修改添加,感觉不是数据库问题,查看监听 ...

  4. 基于Dockfile构建JAVA环境网站镜像

    查看本地目录 [root@docker tomcat]# ls apache-tomcat-8.5.16.tar.gz  Dockerfile  jdk-8u91-linux-x64.tar.gz   ...

  5. Kioptrix Level 2

    简介 Vulnhub是一个提供各种漏洞环境的靶场平台. 个人学习目的:1,方便学习更多类型漏洞.2,为OSCP做打基础. 下载链接 https://www.vulnhub.com/entry/kiop ...

  6. C++ STL 优先队列 (priority_queue)

    std::priority_queue <queue> 优先队列   优先队列是一种容器适配器,根据某些严格的弱排序标准,使其第一个元素始终包含的最大元素.   这种特性类似于堆,它可以在 ...

  7. 爬虫学习(三)Chrome浏览器使用

    一.新建隐身窗口 在打开隐身窗口的时候,第一次请求某个网站是没有携带cookie的,和代码请求一个网站一样,不携带cookie.这样就能够尽可能的理解代码请求某个网站的结果:除非数据是通过js加载出来 ...

  8. 前端知识(二)03-Webpack-谷粒学院

    目录 一.什么是Webpack 二.Webpack安装 1.全局安装 2.安装后查看版本号 三.创建项目 1.初始化项目 2.创建src文件夹 3.src下创建common.js 4.src下创建ut ...

  9. C++中输出变量类型的方法

    C++中输出变量类型的方法 在c++中输出变量或者数据类型,使用typeid().name()的方法.如下例子: #include <iostream> #include <stri ...

  10. 详解SpringMVC

    详解SpringMVC 一.什么是MVC? ​ MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范.就是将业务逻辑.数据.显示分离的方法来组织代码. ...