题目:

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 ;
}

最新文章

  1. Windows Azure HandBook (8) Azure性能测试(1)
  2. Oracle中Cursor的用法
  3. gotoTop返回顶部 JS
  4. mysql基础知识(4)--修改
  5. 如何实现Conditional Include
  6. .NET笔试题(关于迭代的:遍历XML中的FileName)
  7. 16个最棒的jQuery视差滚动效果教程
  8. MySQL的索引
  9. Inter IPP的一些基本类型对应的vs中类型
  10. LR的响应时间与使用IE所感受时间不一致的讨论
  11. .net 正则获取url参数
  12. java表达式类型的自动提升
  13. Java开发笔记(五十一)多态的发生场景
  14. [Android][Recovery] Recovery下找不到sdcard路径
  15. HashMap 和 Hashtable 的 6 个区别,一般人不知道最后一条
  16. 京东饭粒捡漏V1.14
  17. [BOI2007]Mokia 摩基亚(CDQ分治)
  18. MSSQL如何将查询结果拼接成字符串
  19. BZOJ3601 一个人的数论
  20. Linux多线程同步之相互排斥量和条件变量

热门文章

  1. Hammer.js 实现移动端元素的拖拽库
  2. Python中级 —— 02函数式编程
  3. JS中判断字符串中出现次数最多的字符及出现的次数
  4. Java Hibernate Validator
  5. net core mysql 组件记录
  6. armv7学习记录
  7. Leecode刷题之旅-C语言/python-434 字符串中的单词数
  8. 面试和工作中的map
  9. R语言学习笔记—朴素贝叶斯分类
  10. 114. Unique Paths [by Java]