跳到正文

题解

题解 [AGC049C] Robots

Jsxts_约 4 分钟
原文
文章信息与版本

文章信息

发布
2023年7月12日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 4 分钟

版本

  1. v1当前

有趣的思维题。

考虑不能修改时怎么吃小球最优。对于 ai>bia_i>b_i 的球,怎么吃都行,于是若按 ii 从小到大吃掉这些球,数轴上会有若干区间的机器人报废。此时若还有 ai≤bia_i\le b_i 对应的机器人没有报废,那么最终一定会有一个这样的机器人踩到 00。

现在要做最少的操作使得不存在这样的机器人。容易发现,我们可以通过两种方法使一个球对应机器人 ii 满足条件:

  • 在 ai+1a_i+1 的位置放一个球,这样可以先让 ai+1a_i+1 的机器人踩掉 ii,这里如果有一个 ai∼aja_i\sim a_j 都满足 a≤ba\le b 的连续段,就在 j+1j+1 放 j−i+1j-i+1 个球来踩这个区间。

  • 将 aia_i 位置的球数量减少到 ai−1a_i-1,使得 ii 不会踩到 00。

发现第二种方法最多用一次,并且一定在一个 ai≤bia_i\le b_i 的机器人上用。证明:

  • 若 i,j(i<j)i,j(i<j) 都用方法二,显然先让 jj 操作 ii 已经被摧毁,不需要进行操作。

  • 若在一个 ai>bia_i>b_i 的机器人上操作,则你可以对于所有 ≤ai−bi\le a_i-b_i 的 aj≤bja_j\le b_j 的球,对它使用方法一。这样肯定不劣于前者。

所以,我们枚举每种 ai≤bia_i\le b_i 的球,对它使用方法二,并对这之后的 ai≤bia_i\le b_i 且没有被 ai>bia_i>b_i 报废的机器人使用方法一。发现方法二的球和方法一的可以共用,于是答案取最大值即可。

ai≤bia_i\le b_i 且没有被 ai>bia_i>b_i 报废的机器人可以离散化后用差分维护。

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 2e9;
const int N = 2e5;
inline int read() {
	int s = 0,f = 1;char ch = getchar();
	while (!isdigit(ch)) f = ch == '-' ? -1 : 1, ch = getchar();
	while (isdigit(ch)) s = (s << 3) + (s << 1) + ch - '0', ch = getchar();
	return s*f;
}
int n,a[N + 10],b[N + 10],c[N * 2 + 10],h[N * 2 + 10];
int main() {
	n = read();
	for (int i = 1;i <= n;i ++ ) a[i] = h[i] = read();
	int tot = n;
	for (int i = 1;i <= n;i ++ ) {
		b[i] = read();
		if (a[i] > b[i]) h[++tot] = a[i] - b[i];
	}
	sort(h + 1,h + tot + 1);
	tot = unique(h + 1,h + tot + 1) - h - 1;
	for (int i = 1;i <= n;i ++ ) {
		if (a[i] > b[i]) {
			c[lower_bound(h + 1,h + tot + 1,a[i] - b[i]) - h] ++, c[lower_bound(h + 1,h + tot + 1,a[i]) - h + 1] --;
		}
	}
	for (int i = 1;i <= tot;i ++ ) c[i] += c[i - 1];//, cout << i << ' ' << c[i] << endl;
	int tt = 0;
	for (int i = 1;i <= n;i ++ ) {
		if (!c[lower_bound(h + 1,h + tot + 1,a[i]) - h]) tt ++;
	}
	if (!tt) cout << 0, exit(0);
	int ans = tt,tt2 = 0;
	for (int i = 1;i <= n;i ++ ) {
		if (!c[lower_bound(h + 1,h + tot + 1,a[i]) - h]) tt2 ++;
		if (a[i] <= b[i]) ans = min(ans,max(tt - tt2,b[i] - a[i] + 1));
	}
	cout << ans << endl;
	return 0;
}
原文
存档评论4 条抓取于