题解 AT3949 【[AGC022D] Shopping】
标签: 神奇性质.
Part 1
先可以把
由于最终要回到
Part 2
首先我们给答案确定一个上界并构造方案.
我们先走到商场
考虑优化方案以达到最优.
我们设
显然对于
若存在一组
另外我们发现对于
那么我们用
另外, 若
本题有许多细节(可能有的上面都没有提到, 譬如商场
时间复杂度
#include <bits/stdc++.h>
using namespace std;
int read();
int n, L, res;
int x[300005], t[300005], del[300005];
int l[300005], r[300005];
int main() {
n = read(), L = read(), res = n + 1;
for (int i = 1; i <= n; ++i) x[i] = read();
for (int i = 1; i <= n; ++i)
res += (t[i] = read()) / (L << 1), t[i] %= L << 1;
for (int i = 1; i <= n; ++i) {
if (!t[i]) {
--res, del[i] = 1;
continue;
}
del[i] = !((l[i] = 2 * (L - x[i]) >= t[i]) | (r[i] = 2 * x[i] >= t[i]));
}
res -= l[n];
int sum = 0, top = 0, pos = n;
for (int i = 1; i < n; ++i) {
if (del[i]) continue;
if (!l[i]) {
pos = i;
break;
}
r[i] ? ++top : (top ? --top, --res : 0);
}
sum += top, top = 0;
for (int i = n - 1; i >= pos; --i) {
if (del[i]) continue;
if (!r[i]) break;
l[i] ? ++top : (top ? --top, --res : 0);
}
printf("%lld\n", 2ll * L * (res -= sum + top >> 1));
return 0;
}
const int _SIZE = 1 << 22;
char ibuf[_SIZE], *iS = ibuf, *iT = ibuf;
#define gc \
(iS == iT ? iT = ((iS = ibuf) + fread(ibuf, 1, _SIZE, stdin)), \
(iS == iT ? EOF : *iS++) : *iS++)
int read() {
int x = 0, f = 1;
char c = gc;
while (!isdigit(c)) f = (c == '-') ? -1 : f, c = gc;
while (isdigit(c)) x = x * 10 + c - '0', c = gc;
return x * f;
}