網址:
http://codeforces.com/contest/644/problem/B
B. Processing Queries time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard 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 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 12 94 810 915 219 1
output
11 19 -1 21 22
input
4 12 84 810 915 2
output
10 18 27 -1
Note
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 query2 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.
題意:
有一個機器工作,每次只能處理一個任務,等待隊列的最大容量為b,現在有n個任務,給出n個任務出現的時間和需要處理的時間,有以下要求
1,若且唯若機器空閑時,才能處理任務
2,當機器工作時,如果某個時候有了新任務,而且等待隊列少於b,那麼就把任務放在隊尾,否則不放入隊列,不進行處理
另外,如果某個時刻,機器剛好完成了某個任務時,有新任務到來,規定為:機器會從隊列中取元素,然後任務才出現。
輸出每個任務完成的時間,如果任務未進行處理,輸出-1
說白了,完全是類比這個隊列了.........
代碼是很簡單的寫出來了,但是有漏洞啊。
錯了兩次才明白怎麼回事,忘記處理機器已經處理完,但是任務還沒有到來的情況了,結果程式中途跳出了..........
後來加上了這樣的情況的判斷,順利AC了............
比賽,肯定有自己不會做的題目,但是,要保證會做的題一定不要有紕漏,否則本來就不強的實力,再打個折扣,就真心跪了.........
一定要細心細心再細心。
以後遇到簡單的題也要詳細考慮,把一些特殊的資料考慮到,要不然肯定會留下遺憾...(遺憾是肯定會有啦....不過還是盡量少點吧...)
#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",&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())//隊列為空白的時候 { if(i<n)//不能終止 { 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;}