CF1555C题解
-
分析
看着感觉很不可做,因为第一个人走的方式有巨大的后效性。
但是发现 \(n=2\),那么第一个人走的方式一定是先在上面走一段,再在下面走一段(直接走下面视为先在上面走了 \(0\) 个数)
设第一个人在 \(x\) 处从第一行下去,那么第二个人能选择的数之和一定为第一行 \(x + 1\) 的后缀和或者第二行 \(x - 1\) 的前缀和的最大值。
第一个人可以决定 \(x\),他肯定会取后缀和前缀和最大值最小的位置 \(x\)。
线性扫一遍即可。 -
代码
#include <iostream>
using namespace std;
constexpr int MAXN(1000007);
constexpr int INF(0x3f3f3f3f);
int a[MAXN], b[MAXN], sa[MAXN], pb[MAXN];
int T, n;
inline void read(int &temp) { cin >> temp; }
inline void work() {
int ans(INF);
read(n);
for (int i(1); i <= n; ++i) read(a[i]);
for (int i(1); i <= n; ++i) read(b[i]);
sa[n + 1] = 0;
for (int i(n); i; --i) sa[i] = sa[i + 1] + a[i];
for (int i(1); i <= n; ++i) pb[i] = pb[i - 1] + b[i];
for (int i(1); i <= n; ++i) ans = min(ans, max(sa[i + 1], pb[i - 1]));
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
read(T);
while (T--) work();
return 0;
}