I. Convention II(优先队列)

http://codeforces.com/group/NVaJtLaLjS/contest/238203/problem/I
I. Convention II
time limit per test
1 second
【I. Convention II(优先队列)】memory limit per test
256 megabytes
input
standard input
output
standard output
Despite long delays in airport pickups, Farmer John's convention for cows interested in eating grass has been going well so far. It has attracted cows from all over the world.
The main event of the conference, however, is looking like it might cause Farmer John some further scheduling woes. A very small pasture on his farm features a rare form of grass that is supposed to be the tastiest in the world, according to discerning cows. As a result, all of the NN cows at the conference (1≤N≤1051≤N≤105) want to sample this grass. This will likely cause long lines to form, since the pasture is so small it can only accommodate one cow at a time.
Farmer John knows the time aiai that each cow ii plans to arrive at the special pasture, as well as the amount of time titi she plans to spend sampling the special grass, once it becomes her turn. Once cow ii starts eating the grass, she spends her full time of titi before leaving, during which other arriving cows need to wait. If multiple cows are waiting when the pasture becomes available again, the cow with the highest seniority is the next to be allowed to sample the grass. For this purpose, a cow who arrives right as another cow is finishing is considered "waiting". Similarly, if a number of cows all arrive at exactly the same time while no cow is currently eating, then the one with highest seniority is the next to eat.
Please help FJ compute the maximum amount of time any cow might possibly have to wait in line (between time aiai and the time the cow begins eating).
Input
The first line of input contains NN. Each of the next NN lines specify the details of the NN cows in order of seniority (the most senior cow being first). Each line contains aiai and titi for one cow. The titi's are positive integers each at most 104104, and the aiai's are positive integers at most 109109.
Output
Please print the longest potential waiting time over all the cows.
Example
input
Copy

5 25 3 105 30 20 50 10 17 100 10

output
Copy
10

Note
In this example, we have 5 cows (numbered 1..5 according to their order in the input). Cow 4 is the first to arrive (at time 10), and before she can finish eating (at time 27) cows 1 and 3 both arrive. Since cow 1 has higher seniority, she gets to eat next, having waited 2 units of time beyond her arrival time. She finishes at time 30, and then cow 3 starts eating, having waited for 10 units of time beyond her starting time. After a gap where no cow eats, cow 5 arrives and then while she is eating cow 2 arrives, eating 5 units of time later. The cow who is delayed the most relative to her arrival time is cow 3.

题目大意:
有n头牛,给出每头牛的达到时间,和吃草花费的时间,先给出的年长,即优先级高。
只有一块草地,只能一头牛在吃,其他牛若到达,则需等到它吃完,若有多头牛在等,则优先级高的先吃。
求一头牛的最大等待时间。
题目分析:
采用优先队列。先按到达时间从小到大排序。第一头牛到达加入队列中,记录时间=到达时间+吃草时间。
此时判断有多少头牛的到达时间<=记录时间,加入优先队列中。然后在记录时间+=下头牛的吃草时间。
此时判断有多少头牛的到达时间<=记录时间,加入优先队列中。若优先队列为空则看,是否每头牛都吃过草了。
若为否,在加入优先队列中。
牛的等待时间=它吃草时间-到达时间;每次更新ans即可。
优先队列非常实用。及重载运算符要掌握。
代码:
#include using namespace std; const int N=100005; struct Node{ int sta,cost,id; bool operator <(const Node &aa)const{ return aa.id>n; for(int i=1; i<=n; i++) { cin>>a[i].sta>>a[i].cost; a[i].id=i; } sort(a+1,a+1+n,cmp); priority_queueq; int time=0,ans=0,k; for(int i=1; i<=n; i++) { if(q.empty()) { time=a[i].sta+a[i].cost; for(k=i+1; k<=n; k++) { if(a[k].sta<=time) q.push(a[k]); else break; } } else { Node tmp; tmp=q.top(); q.pop(); ans=max(ans,time-tmp.sta); time+=tmp.cost; for(; k<=n; k++) { if(a[k].sta<=time) q.push(a[k]); else break; } } } cout<


    推荐阅读