CF479C 题解

发布时间 2023-08-02 16:37:57作者: So_noSlack

洛谷链接&CF 链接

题目简述

一个人想要安排期末考试的时间。

\(n\) 场考试,每场考试有两个时间 \(x_i,y_i\),一个是老师规定的时间,另外一个是他与老师商量好的考试时间。

如果错过了,那就只能按照原来的考试时间考试。

规定:只能按照原定考试的日期顺序进行考试的情况下,输出考完试的日期。

思路

简单思维题,因为只能按照原定顺序进行考试,所以用结构体储存两个时间点,接着按照 \(x_i\) 的非递减顺序排序,依次遍历,如果当前 \(ans\) 小于 \(y_i\) 更新 \(ans\) 的值即可,否则 \(ans\) 赋值为当前 \(x_i\) 的值。

下面就是代码实现:

#include<iostream>
#include<algorithm> 
using namespace std;

struct node {
	int x, y; // 记录两个时间点
} p[5005]; // 注意数据范围
int n; 

// 排序函数,上面解释过
bool cmp(node a, node b) {
	if(a.x == b.x) return a.y < b.y;
	return a.x < b.x; 
}

int main(){
	cin >> n;
	for(int i = 1; i <= n; i ++) cin >> p[i].x >> p[i].y; // 输入
	sort(p + 1, p + n + 1, cmp); // 按照 x 的非递减顺序排序
	int ans = p[1].y; // 初始化 ans
	for(int i = 1; i <= n; i ++)
		if(ans <= p[i].y) ans = p[i].y; // 上述情况 1
		else ans = p[i].x; // 上述情况 2
	cout << ans << endl; // 输出
	return 0;
}

提交记录

\[\text{The End!} \]