【Codeforces Round 274 (Div 2)C】【贪心】Exams a[i]位置写a[i]或b[i] 所有位置的数保证不下降的最早结束时间

原创 2015年12月08日 15:57:12
C. Exams
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.

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


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=5000+10,M=0,Z=1e9+7,ms63=1061109567;
int n,T;
pair<int,int>a[N];
int dfs(int p)
{
	if(a[p].first<=T)return a[n].second;
	if(a[p].second<=T){T=a[p].second;return dfs(p-1);}
	return a[n].first;
}
bool check()
{
	for(int i=n;i;--i)
	{
		if(a[i-1].first<=a[i].second)return 1;
		if(a[i-1].second>a[i].second)return 0;
	}
	//return 1;
}
int main()
{
	while(~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);
		//T=a[n].second;printf("%d\n",dfs(n-1));
		printf("%d\n",check()?a[n].second:a[n].first);
	}
	return 0;
}
/*
【trick&&吐槽】
这道题非常有趣,2333

【题意】
给你n(1<=n<=5000)个pair,
对于一个pair(a[i],b[i]),一定有a[i]>b[i],它表示——
我们可以在位置a[i]处,写下一个数,数值可以选择为a[i]或b[i]。
我们希望在所有位置写下的数,按照位置保证升序。
问你,我们究竟要怎么选数,可以使得最后一个数尽可能小。

【类型】
脑洞题

【分析】
这题首先一定是有解的,即我们至少有一种方案,使得数值按照位置保证升序关系。
因为我们可以直接在位置a[i]处写下数值a[i]。

这题想要使得最后一个位置的数尽可能小。
于是,对于最后一个位置a[n],我们这个位置的数可以写b[n]。
然而,写b[n]并不一定是合法的。我们需要使得,在它前面位置的数,数值都比b[n]小。

如果a[n-1]<=b[n],a[n-1]处的数值可以为a[n-1],已经不再影响前效性。答案显然是b[n]
否则a[n-1]处就不能取a[n-1],如果b[n-1]>b[n],那么显然非法;
						   如果b[n-1]<=b[n],还是有可能合法的,合法性通过前溯n-2与n-1的关系求得。

整体的实现,我们可以通过一个dfs实现。
当然这个可以也用for循环实现2333

【时间复杂度&&优化】
O(n)

*/

版权声明:题解中哪里写错请一定要指出来QwQ 转载还请注明下出处哦,谢谢^_^

相关文章推荐

【CODEFORCES】 A. Exams

A. Exams time limit per test 1 second memory limit per test 256 megabytes input st...
  • S_Black
  • S_Black
  • 2015年08月03日 16:52
  • 354

【Codeforces Round 336 (Div 2) C】【贪心 DP思维】Chain Reaction 每个灯塔位置为a[]破坏b[]范围所有灯塔 设置一个灯塔使得最多灯塔被保留

C. Chain Reaction time limit per test 2 seconds memory limit per test 256 megabytes ...

Codeforces Round #274 (Div. 2) c Exams

点击打开链接链接 C. Exams time limit per test 1 second memory limit per test 256 megabyte...

【Codeforces Round 274 (Div 2)B】【贪心】Towers 若干次移数后使得最大差值尽可能小

B. Towers time limit per test 1 second memory limit per test 256 megabytes input s...

Codeforces Round #274(Div2) B. Towers 贪心

解法一:复杂度为O(nk) 每次从最大值向最小值移动1个 代码如下: #include using namespace std; #define N 1005 int a[105], ans[N][...
  • ww32zz
  • ww32zz
  • 2015年07月15日 14:04
  • 266

Codeforces Round #221 (Div. 2) B. I.O.U.

B. I.O.U. time limit per test 1 second memory limit per test 256 megabytes input s...

【Codeforces Round 271 (Div 2)B】【前缀和二分 or 暴力】Worms 每种类型多个个数第i个数什么类型

B. Worms time limit per test 1 second memory limit per test 256 megabytes input st...

Codeforces Round #274 (Div. 2) A,B,C

A. Expression time limit per test 1 second memory limit per test 256 megabytes inpu...

【Codeforces Round 336 (Div 2) A】【贪心 水题】Saitama Destroys Hotel 电梯只下不上 接n人到达0层最早时刻

A. Saitama Destroys Hotel time limit per test 1 second memory limit per test 256 megabyt...

【Codeforces Round 336 (Div 2) B】【水题 前缀和】Hamming Distance Sum 双字符串所有位置匹配下的差异数之和

B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabyte...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【Codeforces Round 274 (Div 2)C】【贪心】Exams a[i]位置写a[i]或b[i] 所有位置的数保证不下降的最早结束时间
举报原因:
原因补充:

(最多只允许输入30个字)