题目:

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly nexams. 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.

Examples
input
3
5 2
3 1
4 2
output
2
input
3
6 1
5 2
4 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.

题解:

先按a的值排序,排序完后,考虑最后一个考试要不要换,如果换了能贪心出来非降序则换,否则就不换。

 #include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std; const int maxn=+; int n; struct Node{
int x,y;
bool operator < (const Node& tmp) const {
return x>tmp.x||(x==tmp.x)&&y>tmp.y;
}
}nds[maxn]; int main() {
// freopen("data_in.txt","r",stdin);
while (scanf("%d",&n)==&&n) {
for(int i=;i<n;i++){
scanf("%d%d",&nds[i].x,&nds[i].y);
}
sort(nds,nds+n);
int su=;
swap(nds[].x,nds[].y);
for(int i=;i<n;i++){
if(nds[i].y>nds[i-].x){
su=;
break;
}
if(nds[i].x>nds[i-].x){
swap(nds[i].x,nds[i].y);
}
}
int ans;
if(su) ans=nds[].x;
else ans=nds[].y;
printf("%d\n",ans);
}
return ;
}

CodeForces 479C Exams 贪心的更多相关文章

  1. codeforces 479C Exams 解题报告

    题目链接:http://codeforces.com/problemset/problem/479/C 题目意思:简单来说,就是有个人需要通过 n 门考试,每场考试他可以选择ai, bi 这其中一个时 ...

  2. CodeForces - 158B.Taxi (贪心)

    CodeForces - 158B.Taxi (贪心) 题意分析 首先对1234的个数分别统计,4人组的直接加上即可.然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一 ...

  3. Codeforces Round #280 (Div. 2) C. Vanya and Exams 贪心

    C. Vanya and Exams Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/492/pr ...

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

    A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...

  5. codeforces 480A A. Exams(贪心)

    题目链接: A. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. Codeforces Round #377 (Div. 2) D. Exams 贪心 + 简单模拟

    http://codeforces.com/contest/732/problem/D 这题我发现很多人用二分答案,但是是不用的. 我们统计一个数值all表示要准备考试的所有日子和.+m(这些时间用来 ...

  7. Codeforces Round #481 (Div. 3) G. Petya's Exams (贪心,模拟)

    题意:你有\(n\)天的时间,这段时间中你有\(m\)长考试,\(s\)表示宣布考试的日期,\(d\)表示考试的时间,\(c\)表示需要准备时间,如果你不能准备好所有考试,输出\(-1\),否则输出你 ...

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

    题意:给\(n\)场考试的时间,每场考试可以提前考,但是记录的是原来的考试时间,问你如何安排考试,使得考试的记录时间递增,并且最后一场考试的时间最早. 题解:因为要满足记录的考试时间递增,所以我们用结 ...

  9. codeforces 724D(贪心)

    题目链接:http://codeforces.com/contest/724/problem/D 题意:给定一个字符串和一个数字m,选取一个一个子序列s,使得对于字符串中任意长度为m的子序列都至少含有 ...

随机推荐

  1. JavaScript中的this详解(彻底弄懂js中的this用法)!

    要想学好js,那么其中那些特别令人混淆迷惑的知识点,就一定要弄清楚.this关键字就是其中让初学者比较迷惑的知识点之一,不过灵活运用this可以提升代码的性能和复用性,那么今天我就和大家一起来了解th ...

  2. 大数据分析系统Hadoop的13个开源工具

    Hadoop是由Apache基金会开发的一个大数据分布式系统基础架构,最早版本是2003年原Yahoo!DougCutting根据Google发布的学术论文研究而来. 用户可以在不了解分布式底层细节的 ...

  3. PHP-学习笔记-进阶

    PHP-学习笔记-进阶 PHP类和对象之定义类的方法 访问控制的关键字代表的意义为: public:公开的 protected:受保护的 private:私有的 我们可以这样定义方法: class C ...

  4. 开了几天的phpmyadmin的总结

    近来无事,免费体验了一波腾讯云的vps,打了一个phpstudy,全部默认的配置,只不过,没有给他写入文件的权限 开启了日志,看了下,这几天黑客们的活动 首先,有两三个ip来爆破我的phpmyadmi ...

  5. 20155215 《Java程序设计》实验二( Java面向对象程序设计)实验报告

    20155215 <Java程序设计>实验二( Java面向对象程序设计)实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉 ...

  6. 20155307 2017-2018-3 《Java程序设计》第3周学习总结

    20155307 2017-2018-3 <Java程序设计>第3周学习总结 教材学习内容总结 类相当于是设计图,对象是根据类设计出来的.用class定义,名字叫clothes.可以用ne ...

  7. 创龙6748开发板加载.out出现a data verification error occurred, file load failed

    1. 需要提前添加GEL文件 2. 找到GEL文件路径 3. 然后再加载.out文件

  8. selenium +java 多个类公用driver问题

    问题点:太久没有写selenium代码,居然把driver公用的问题忘记了,即:每写一个测试类,执行过程中都会新建一个窗口,这样应该说是非常不专业的. 大概想了一个方法,虽然看起来也不怎么专业,但感觉 ...

  9. NO.06--聊一聊“币”吧!

    近期博主更新的频率明显慢来 ,一来是最近的工作比较忙碌,几个项目几乎同时要上线.二来是在思考是不是把我平时生活中的一些事情写进来博客,不只是分享分享技术. 趁着区块链.比特币火爆,博主也算是略有涉猎, ...

  10. HDU-4055:Number String

    链接:HDU-4055:Number String 题意:给你一个字符串s,s[i] = 'D'表示排列中a[i] > a[i+1],s[i] = 'I'表示排列中a[i] < a[i+1 ...