Description
Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in one of its other hotels. The elevator is special — it starts on the top floor, can only move down, and has infinite capacity. Floors are numbered from 0 to s and elevator initially starts on floor s at time 0.
The elevator takes exactly 1 second to move down exactly 1 floor and negligible time to pick up passengers. Genos is given a list detailing when and on which floor passengers arrive. Please determine how long in seconds it will take Genos to bring all passengers to floor 0.
Input
The first line of input contains two integers n and s (1 ≤ n ≤ 100, 1 ≤ s ≤ 1000) — the number of passengers and the number of the top floor respectively.
The next n lines each contain two space-separated integers fi and ti (1 ≤ fi ≤ s, 1 ≤ ti ≤ 1000) — the floor and the time of arrival in seconds for the passenger number i.
Output
Print a single integer — the minimum amount of time in seconds needed to bring all the passengers to floor 0.
Sample Input
3 7 2 1 3 8 5 2
11
5 10 2 77 3 33 8 21 9 12 10 64
79
Hint
In the first sample, it takes at least 11 seconds to bring all passengers to floor 0. Here is how this could be done:
1. Move to floor 5: takes 2 seconds.
2. Pick up passenger 3.
3. Move to floor 3: takes 2 seconds.
4. Wait for passenger 2 to arrive: takes 4 seconds.
5. Pick up passenger 2.
6. Go to floor 2: takes 1 second.
7. Pick up passenger 1.
8. Go to floor 0: takes 2 seconds.
This gives a total of 2 + 2 + 4 + 1 + 2 = 11 seconds.
题意:有一个电梯它只能从上往下开
现在已知每个人所在楼层已经上电梯的时间
求最早到达0层的时间
思路:先按楼层从大到小排序,再判断是否需要等待,如果要,再加上等待的时间就行了#include<stdio.h>
#include<algorithm>
using namespace std;
struct s
{
int x,y;
}a[1005];
bool cmp(const s &a,const s &b)
{
return a.x>b.x;
}
int main()
{
int n,s;
while(~scanf("%d%d",&n,&s))
{
int sum=0,i;
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a,a+n,cmp);
for(i=0;i<n;i++)
{
if(i==0)
{
sum+=s-a[i].x;
if(sum<=a[i].y)
sum+=a[i].y-sum;
}
else
{
sum+=a[i-1].x-a[i].x;
if(sum<=a[i].y)
sum+=a[i].y-sum;
}
}
sum+=a[n-1].x;
printf("%d\n",sum);
}
return 0;
}