题解 [AGC049C] Robots
Jsxts_
·
·
题解
有趣的思维题。
考虑不能修改时怎么吃小球最优。对于 a_i>b_i 的球,怎么吃都行,于是若按 i 从小到大吃掉这些球,数轴上会有若干区间的机器人报废。此时若还有 a_i\le b_i 对应的机器人没有报废,那么最终一定会有一个这样的机器人踩到 0。
现在要做最少的操作使得不存在这样的机器人。容易发现,我们可以通过两种方法使一个球对应机器人 i 满足条件:
发现第二种方法最多用一次,并且一定在一个 a_i\le b_i 的机器人上用。证明:
-
若 i,j(i<j) 都用方法二,显然先让 j 操作 i 已经被摧毁,不需要进行操作。
-
若在一个 a_i>b_i 的机器人上操作,则你可以对于所有 \le a_i-b_i 的 a_j\le b_j 的球,对它使用方法一。这样肯定不劣于前者。
所以,我们枚举每种 a_i\le b_i 的球,对它使用方法二,并对这之后的 a_i\le b_i 且没有被 a_i>b_i 报废的机器人使用方法一。发现方法二的球和方法一的可以共用,于是答案取最大值即可。
```cpp
#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;
}
```