您的位置:首页 > 其它

CF#274 (Div. 2) C.(贪心+排序)

2014-10-21 00:13 375 查看
C. Exams

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

题目链接:http://codeforces.com/contest/479/problem/C

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 test(s)

Input
3
5 2
3 1
4 2


Output
2


Input
3
6 1
5 24 3


Output
6


Note
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.

解题思路:

题意是某人有n科考试,每科考试给a和b两个值。a代表老师规定的考试日期,b代表在第b天他也可以去考试。也就是说他只能选择在第a天或第b天去考试。求最后一科

考试日期的最小值。老师会给他记录考试日期,所有课按规定的a记录,要求最后的到的记录是升序排列。

把a按升序排列,a相同的,b小的放前面。然后res初始为1,如果当前的b大于等于res,那么更新res;否则,用当前的a更新res。就是每次花尽可能小的天数来更新res,这样求出的res就是最小的结束日期。

完整代码:

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")

typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;

/** Constant List .. **/ //{

const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;

struct node
{
    int a ;
    int b;
}q[10001];

bool cmp(node x , node y)
{
    if(x.a == y.a)
        return x.b < y.b;
    return x.a < y.a;
}

int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    int n ;
    while(~scanf("%d",&n))
    {

        for(int i = 0 ; i < n ; i ++ )
        {
            scanf("%d%d",&q[i].a,&q[i].b);
        }

        sort(q , q + n , cmp);

        int res = 1;
        for(int i = 0 ; i < n ; i ++)
        {
            if(q[i].b >= res)
                res = q[i].b;
            else
                res = q[i].a;
        }
        printf("%d\n",res);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐