A - A
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.
According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student‘s record book on the day of the actual exam and write down the date of the mark as number ai.
Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date.
Input
The first line contains a single positive integer n (1 ≤ n ≤ 5000) — the number of exams Valera will take.
Each of the next n lines contains two positive space-separated integers ai and bi (1 ≤ bi < ai ≤ 109) — the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly.
Output
Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.
Sample Input
Input
35 23 14 2
Output
2
Input
36 15 24 3
Output
6
Hint
In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5.
In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
题意是说一个学生要参加考试,正常第I科的考试是在第a[i]天举行,但是可以提前到b[i]天(b[i]<a[i])去考。
这货是个学神,考试不会不通过,一天也可以参加无限多场考试。
然后考完第I科,老师会在记录本上记下这场考试的实际上的考试日期(也就是说即使我提前在b[i]天考了,老师在记录本上记下的日期仍然是a[i])
要求记录本上的日期(也就是a[i])非减,问最快可以什么时间参加完最后一科考试。
分析:能大概感觉到是个贪心。既然限制是a[i]要非减,那么我们就以a[i]为关键字进行排序。
然后扫一遍,因为每个b[i]都要比a[i]小,所以优先选择让这货在b[i]pass掉第I科目,如果条件不允许,则在a[i]天过掉。
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int n,ans; const int N=1E4+5; int a[N],b[N]; struct Q {int a,b; }q[N]; bool cmp(Q x, Q y) { if ( x.a<y.a) return true; if ( x.a==y.a &&x.b<y.b ) return true; return false; } int main() { scanf("%d",&n); for ( int i = 1 ; i <= n ; i++ ) scanf("%d %d",&q[i].a,&q[i].b); sort(q+1,q+n+1,cmp); ans=q[1].b; for ( int i = 2 ; i <= n; i++ ) { if ( q[i].b>=ans ) ans = q[i].b; else ans = q[i].a; } printf("%d\n",ans); return 0; }