R1_I_Convention II

题面

I. Convention II

time limit per test

1 second

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

5
25 3
105 30
20 50
10 17
100 10

output

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 N N头牛和一片草地,每头牛都有一个到达的时间和吃草所需的时间,若有一个牛在吃草,那么到的牛就需要等它吃完,而且若是有两个牛以上在等,那么下一个可以吃草的牛不是先到的那个,而是按照题目给出的先后顺序来决定谁先吃。问最大的等待时间。

题目分析

模拟题。

先对牛按照到达的顺序排序(记录下读入的先后顺序),然后第一个牛入列,计算吃完的时候的时间,出列。这时到达时间小于这个吃完的时候的时间的,入列。按照读入的先后顺序出列,继续处理下一个牛,如此循环,直到所有的牛都出列了。入列的时候是按照先后到达的时间,出列是按照读入的先后顺序,这无疑是一个优先队列。结构体里重定义好大小关系就行。注意,每跟新一下现在的时间,都要检查一遍是否有牛要入列,而且优先队列里为空且下一个牛到达的时间比现在的时间要大的话,直接跟新现行时间为下一个牛到达时间。

代码

#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1e5 + 7;
typedef struct Node{
  int num, a, t;
  bool operator < (const Node &x) const{
    return num > x.num;
  }
}node;
node arr[maxn];
priority_queue<node> que;
bool cmp(node a, node b){
  if(a.a == b.a)
    return a.num < b.num;
  else
    return a.a < b.a;
}
int main(int argc, char const *argv[]) {
  int n;
  scanf("%d", &n);
  for(int i = 0; i < n; i++){
    int a, t;
    scanf("%d%d", &a, &t);
    arr[i].num = i; arr[i].a = a; arr[i].t = t;
  }
  sort(arr, arr + n, cmp);
  int i = 0, now = 0, ans = 0, cnt = 0;
  while(cnt < n){
    if(que.empty())
      now = arr[i].a;

    while(que.size()){
      node top = que.top();
      que.pop(); cnt++;
      ans = max(ans, now - top.a);
      now += top.t;
      while(arr[i].a <= now && i < n){
        que.push(arr[i]);
        i++;
      }
    }
    while(arr[i].a <= now && i < n){
      que.push(arr[i]);
      i++;
    }
  }
  printf("%d\n", ans);
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值