codeforces Round #274(div2) C解题报告

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

题目大意:

      Valera参加期末考试,某场考试i,学校的安排是第ai天考,但老师可以让他bi天考,但老师在表格上填写的还是第ai天考。Valera想要表格上填写的是单调递增序列,并且最早考完,Valera很聪明,每门课都可以过,并且一天可以考多门。

解法:

由于写到表格上的日期始终是ai,要想表格上的日期单调不递减,就必须要按照ai单调不递减的序列的顺序来考试。

同时真正考试的日期,也必须要是单调不递增序列。

这道题目就化成在ai单调不递减的情况下,构造出一个每个i-th由{ai, bi}其中一个组成的单调不递减序列。

代码:

#include <cstdio>
#include <algorithm>
#define N_max 5010

using namespace std;

struct typ1{
	int a, b;
}d[N_max];

int ans, n;

bool cmp(typ1 a, typ1 b) {
	if (a.a < b.a)  
		return true;
	else if (a.a == b.a && a.b < b.b)
		return true;

	return false;
}

void init() {
	scanf("%d", &n);

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

	sort(d+1, d+n+1, cmp);
}

void solve() {
	ans = 0;

	for (int i = 1; i <= n; i++)
		if (d[i].b >= ans)
			ans = d[i].b;
		else
			ans = d[i].a;

	printf("%d\n", ans);
}

int main() {
	init();
	solve();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值