Codeforces Round #274 (Div. 2) C. Exams (贪心)

C. Exams

Time Limit: 1 Sec  

Memory Limit: 256 MB

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

3
5 2
3 1
4 2

Sample Output

2



题意:有n门功课,你可以选择在第a天做,也可以选择在第b天做,但是做的顺序中a必须是非递减的,问需要最少的
天数

思路:结构体对a进行排序,因为a必须是非递减的,所以说排序后直接扫就好了,比较当前需要天数和第i门课程最早
能完成的时间比较,如果大了,那么选择a,如果小了,那么选择min(a,b)。


ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0x7fffffff 
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
struct s
{
	int a,b;
}p[MAXN];
bool cmp(s aa,s bb)
{
	if(aa.a==bb.a)
	return aa.b<bb.b;
	return aa.a<bb.a;
}
int main()
{
	int n; 
	while(scanf("%d",&n)!=EOF)
	{
	    for(int i=0;i<n;i++)
	    scanf("%d%d",&p[i].a,&p[i].b);
	    sort(p,p+n,cmp);
	    int ans=-INF;
	    for(int i=0;i<n;i++)
	    {
	    	int k=min(p[i].a,p[i].b);
	    	if(k<ans)
	    	ans=p[i].a;
	    	else
	    	ans=k;
		}
	    printf("%d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(Codeforces Round #274 (Div. 2) C. Exams (贪心))