Codeforces Round #274 (Div. 1) A. Exams 贪心

A. Exams

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/480/problem/A

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

HINT

题意

在期末,有一个人准备完成n(n<=5000)门考试,每门考试你可以选择在第ai天考试,也可以在第bi天考试。

但是不管怎么样,最后你所考试的顺序的a[i],一定是非降的

问你,最少第几天能够考完所有试

题解:

贪心就好了,第一维排序,然后第二位排序

然后贪心选最小就行了

代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;

pair<int,int> a[5005];

int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&a[i].first,&a[i].second);
    sort(a+1,a+n+1);
    int ans = a[1].second;
    for(int i=1;i<=n;i++)
    {
        if(a[i].second >= ans)
            ans = min(a[i].second,a[i].first);
        else
            ans = a[i].first;
    }
    printf("%d
",ans);
}
原文地址:https://www.cnblogs.com/qscqesze/p/4904518.html