题解 | #C Future Machine#

Future Machine

https://ac.nowcoder.com/acm/contest/69029/C

简单分类讨论题。

显然对 扫描线,假设当前枚举到的为

首先要知道一个基本的事实,那就是一旦 变成了 在本次循环就不会变成其他值。

分成四类讨论:

  1. ,也就是存在 ,这种情况下 一定会变成
  2. ,也就是不存在 ,这种情况下 会变成所有 中, 的最小值和
  3. ,这种情况下 会变成对于所有 中的 的最大值和
  4. ,显然 变为
#include <bits/stdc++.h>
using i64 = long long;

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);

	int n, m, X;
	std::cin >> n >> m >> X;
	std::vector<int> x(n);
	std::vector<int> y(m);

	for (int &a : x)
		std::cin >> a;
	for (int &b : y)
		std::cin >> b;
	std::sort(y.begin(), y.end());

	for (int i = 0; i < n; i++) {
		int pos = std::upper_bound(y.begin(), y.end(), x[i]) - y.begin() - 1;
		if (X > x[i]) {
			// 存在 <= 时
			if (pos > -1)
				X = x[i];
			// 如果所有 y 都大于 x[i]
			else
				X = std::min(y[0], X);
		} else {
			// 全是 <= 时
			if (pos == y.size() - 1)
				X = std::max(y[pos], X);
			// 存在 > 时
			else
				X = x[i];
		}
	}
	std::cout << X << '\n';
	return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务