QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#591096 | #8057. Best Carry Player 4 | zhutianrui | TL | 0ms | 15468kb | C++14 | 1.4kb | 2024-09-26 14:05:38 | 2024-09-26 14:05:40 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define F(i, a, b) for (int i = (a); i <= (b); i++)
#define dF(i, a, b) for (int i = (a); i >= (b); i--)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 500005, M = (N << 1), inf = 1e16, mod = 1e9 + 7;
int n, m, a[N], b[N], c[N], d[N];
void solve(int TC) {
cin >> m;
int mxa = 0, mxb = 0;
F(i, 0, m - 1) {
cin >> a[i], c[i] = 0;
if (a[i]) mxa = i;
}
F(i, 0, m - 1) {
cin >> b[i], d[i] = 0;
if (b[i]) mxb = i;
}
if (mxa + mxb < m) {
cout << "0\n";
return;
}
if (accumulate(a, a + m, 0ll) > accumulate(b, b + m, 0ll))
swap(a, b);
a[0] = b[0] = inf;
int cnt = 0, jin = 0;
F(i, 0, m - 1) {
int x = min(a[i], b[m - 1 - i]);
a[i] -= x, b[m - 1 - i] -= x;
cnt += x;
if (x) c[i] = d[m - 1 - i] = 1;
}
dF(i, m - 2, 0) c[i] |= c[i + 1], d[i] |= d[i + 1];
for (int i = m - 1, j = 1; ~i ; --i) {
while (a[i] && i + j >= m && j < m) {
int x = min(a[i], b[j]);
a[i] -= x, b[j] -= x, jin += x;
if (!b[j]) j++;
}
}
if (jin) {
cout << jin + cnt << '\n';
} else {
int flag = 0;
F(i, 1, m - 1) if (a[i] && d[m - i] || b[i] && c[m - i]) flag = 1;
cout << cnt - 1 + flag << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
for (int i = 1; i <= T; i ++) {
solve(i);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15468kb
input:
5 2 1 2 3 4 3 1 0 1 0 1 0 4 1 0 0 1 1 1 1 1 5 123456 114514 1919810 233333 234567 20050815 998244353 0 0 0 10 5 3 5 3 2 4 2 4 1 5 9 9 8 2 4 4 3 5 3 0
output:
5 1 2 467900 29
result:
ok 5 number(s): "5 1 2 467900 29"
Test #2:
score: -100
Time Limit Exceeded
input:
100000 5 0 1 1 1 1 0 0 1 0 0 5 0 0 0 0 0 1 1 1 0 0 5 0 0 2 1 1 0 2 1 0 1 5 0 0 0 0 0 1 2 1 0 0 5 0 1 0 1 1 0 0 1 1 1 5 2 0 0 0 1 1 0 0 0 3 5 2 0 0 1 1 0 2 1 1 1 5 0 0 0 0 2 0 0 0 0 1 5 0 0 0 0 0 0 1 1 0 0 5 4 0 0 0 0 0 0 0 1 0 5 0 0 0 0 1 2 1 1 0 0 5 0 2 3 0 0 0 0 0 1 0 5 1 1 1 0 1 1 0 1 0 1 5 0 0 0...
output:
2 0 4 0 3 3 3 2 0 0 1 1 3 0 3 0 0 0 0 0 0 0 3 0 4 1 0 2 4 3 1 5 0 0 1 0 0 -1 -1 0 0 2 3 2 2 -1 2 0 1 2 2 -1 0 4 0 1 1 1 0 1 0 4 0 0 2 2 0 3 2 0 2 0 1 0 0 1 1 2 0 3 4 0 1 5 0 2 1 0 0 0 3 2 3 0 2 0 4 3 2 0 2 1 0 -1 3 1 -1 0 0 0 1 0 3 1 1 0 2 1 -1 0 1 0 0 2 3 1 3 2 1 2 1 0 2 0 0 2 3 1 3 1 0 2 1 3 0 1 2...