【CROC 2016 — QualificationB】【队列模拟】Processing Queries 按时间顺序处理任务 队列长度不超过siz
来源:互联网 发布:手机传软件 编辑:程序博客网 时间:2024/05/20 14:14
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 12 94 810 915 219 1
11 19 -1 21 22
4 12 84 810 915 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<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 2e5+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;int casenum, casei;int n, siz;struct A{LL t, d;}a[N];queue<int>q;LL ans[N];int main(){while(~scanf("%d%d",&n,&siz)){for (int i = 1; i <= n; ++i){scanf("%lld%lld", &a[i].t,&a[i].d);}LL T = 0;//T表示上个任务的完成时间while (!q.empty())q.pop();for (int i = 1; i <= n; ++i){//我们把a[i].t之前时间的任务先做完while(!q.empty()){int o = q.front();if (T <= a[i].t) {T = ans[o] = max(T, a[o].t) + a[o].d;q.pop();}else break;}//如果队列空了,我们直接处理这个任务if (T<=a[i].t&&q.empty()){T = ans[i] = a[i].t + a[i].d;continue;}//如果队列没空,我们考虑加入队列if (q.size() < siz)q.push(i);else ans[i] = -1;}while (!q.empty()){int o = q.front();LL finish = max(T, a[o].t) + a[o].d;T = ans[o] = finish;q.pop();}for (int i = 1; i <= n; ++i)printf("%lld ", ans[i]);puts("");}return 0;}/*【trick&&吐槽】小心爆int【题意】有n(2e5)个询问。队列上限为b(2e5)询问按照时间顺序给你。每个询问的时间都不同,分别是ti每个询问需要处理的时间分别是di对于每个询问。1,如果机器空闲,且没有要处理的询问,则立即处理。2,如果机器工作,且要处理的询问数小于b,则加入队尾3,如果机器工作,且要处理的询问数等于b,则丢弃任务。当机器处理完某个询问,它会从队列中选择一个新的做处理。如果这时刚好有新任务,这个任务的优先度在后。输出每个任务的完成时间。【类型】队列模拟【分析】我们只要模拟就好了。在处理每个工作到来的时候,先把对列中在它时间之间的处理。这样才能决定这个工作的目标状态。*/
- 【CROC 2016 — QualificationB】【队列模拟】Processing Queries 按时间顺序处理任务 队列长度不超过siz
- CROC 2016 - Qualification-B. Processing Queries(队列)
- CodeForces 644 B.Processing Queries(队列)
- tyvj 1305 —— 长度不超过m的最大连续和 【前缀和+单调队列】
- 长度不超过k的最大连续子序列(单调队列)
- codeforces644B. Processing Queries (模拟)
- Shell模拟多线程/任务队列
- JavaScript 函数队列按时间间隔顺序执行
- 队列——顺序队列
- codeforces B. Processing Queries【模拟】
- 定时任务 + queue队列处理
- 基于队列多任务处理
- 长度不超过m的最大连续子序列(dp + 单调队列)
- 分布式后台任务队列模拟(Golang)
- 百度前端技术学院-任务四模拟队列
- 【c++】模拟实现顺序队列方法一
- 【c++】模拟实现顺序队列方法二
- 数据结构——队列之顺序队列
- 【April Fools Day Contest 2014H】【愚人节脑洞】A + B Strikes Back 连续提交5次才能AC
- Android音量控制调节
- bzoj 4238: 电压 dfs
- Python清屏小结
- 【CROC 2016 — QualificationA】【水题】nm棋盘填人奇偶不相邻
- 【CROC 2016 — QualificationB】【队列模拟】Processing Queries 按时间顺序处理任务 队列长度不超过siz
- java 中的printStackTrace()方法
- maven构建项目若干问题整理
- 问题 H: 2.4.5 Fractions to Decimals 分数化小数
- OC中Foundation框架字符串NSString和NSMutableString的使用
- android studio gradle 空项目
- ERROR 1840 (HY000) at line 24: GTID_PURGED can only be set when GTID_EXECUTED is empty.
- 第三周项目(4):百鸡问题
- JavaScript 事件模型