Url:
Http://codeforces.com/contest/644/problem/B
B. Processing Queries time limit/test 5 seconds Memory limit per test 256 megabytes input standard input output Standa RD output
In this problem your have to simulate the workflow of One-thread server. There are n queries to process, the i-th'll be received at moment Ti and needs to is processed for DI units of. All ti are guaranteed to be distinct.
When a query appears server could react in three possible ways:if server are free and query \ is empty, then server Imme Diately starts to process this query. If server is busy and there are less than B queries in the queue, then new query are 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 would 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 con Sider that this is picked from the queue and only then new query appears.
For all query find the moment when the server would finish to process it or print-1 if this query would be rejected. Input
The "I" of the input contains two integers n and b (1≤n, b≤200)-the number of queries and the maximum POS Sible 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 was the moment of time when the i-th query Appears and Di are the time server needs to process it. It 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'll finish to process the i-th query (Queries are numbered in the order they appear in the input) or-1 if the corresponding query would be rejected. Examples input
5 1
2 9
4 8
a
9 2 19
1
Output
11 19-1 21 22
Input
4 1
2 8
4 8
Ten 9
15 2
Output
10 18 27-1
Note
Consider. The server'll start to process I at the Moment 2 and'll finish to process it at the MOMENT&NBSP;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 query2 pending in the queue , so third the query is just rejected. At the Moment 11 server'll finish to process in 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 Quer Y appears. As is said in the statement above, the "I" and "second" query, then it'll pick the fourth que Ry from the queue and only then'll 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.
Meaning
There is a machine that can only handle one task at a time, waiting for the maximum capacity of the queue is B, now has n tasks, given the number of times the occurrence of n tasks and the time to deal with the following requirements
1, the task can be handled only when the machine is idle
2, when the machine is working, if there is a new task at some time, and the waiting queue is less than B, then put the task at the end of the team, otherwise not put into the queue, do not process
In addition, if at some point the machine just completes a task, a new task arrives, which says that the machine will take elements from the queue and the task will appear.
Outputs the time each task completes, and if the task is not processed, output-1
To be frank, it is completely analog to this queue ...
The code is very simple to write out, but there are loopholes ah.
Wrong two times before understand how, forget processing machine has been processed, but the task has not come to the situation, the results of the program midway out ...
Then added to the judgment of such a situation, the smooth ac ...
Competition, there must be a problem of their own will not do, but to ensure that the problem will be done must not have flaws, otherwise not strong strength, and then make a discount, really kneel ...
You must be careful and careful.
Later encountered a simple problem to be considered in detail, some special data to take into account, or will certainly leave a regret ... (unfortunately there will be ....) But just try to be less ... )
#include <stdio.h> #include <queue> #include <algorithm> using namespace std;
typedef long Long LL;
struct Task {ll bg,last,time;
ll num;
}X[200005];
int main () {ll n,b;
Freopen ("Shuju.txt", "R", stdin); while (~SCANF ("%i64d%i64d", &n,&b)) {for (int i=0;i<n;++i) {scanf ("%i64d%i64d", &
Amp;x[i].bg,&x[i].last);
X[i].num=i;
} queue<task> Q;
ll time=0,i=1,cnt=0;
Q.push (X[0]);
while (1) {task St=q.front (); Q.pop ();
X[st.num].time=time=st.time=max (st.bg,time) +st.last;
printf ("time=%d\n", time);
while (I<n&&x[i].bg<time) {if ((ll) q.size () >=b) {
X[i].time=-1;
else {Q.push (x[i]);
} ++i;
} if (Q.empty ())//queue is empty {if (i<n)//cannot terminate {Q.push (x[i++]);
Continue
} break;
} printf ("%i64d", x[0].time);
for (ll I=1;i<n;++i) {printf ("%i64d", x[i].time);
printf ("\ n");
return 0; }