QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#739522#8057. Best Carry Player 4UESTC_NLNS#WA 51ms5652kbC++172.0kb2024-11-12 22:00:222024-11-12 22:00:26

Judging History

This is the latest submission verdict.

  • [2024-11-12 22:00:26]
  • Judged
  • Verdict: WA
  • Time: 51ms
  • Memory: 5652kb
  • [2024-11-12 22:00:22]
  • Submitted

answer

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 5e5 + 5, INF = 0x3f3f3f3f;
int T, n, s1, s2, p, tmp;
ll ans, sa, sb, a[N], b[N];
set<int> s;
bool flag;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> T;
    while (T--) {
        ll mn = 1e9;
        cin >> n;
        s.clear();
        p = INF;
        sa = sb = 0;
        flag = false;
        for (int i = 0; i < n; i++) {
            cin >> a[i], sa += a[i];
            if (a[i] != 0) {
                mn = min(mn, (ll)i);
            }
        }
        for (int i = 0; i < n; i++) cin >> b[i], sb += b[i];
        if (sa > sb)
            b[0] += sa - sb;
        else
            a[0] += sb - sa;
        for (int i = 0; i < n; i++)
            if (b[i]) s.insert(i);
        for (int i = n - 1; i >= 1; i--) {
            if (!a[i]) continue;
            auto it = s.lower_bound(n - i);
            if (it == s.end()) continue;
            tmp = (i + (*it)) - n;
            if (tmp < p) p = tmp, s1 = i, s2 = *it;
        }
        if (p == INF) {
            cout << "0\n";
            continue;
        }
        ans = 0;
        for (int i = 0, j = n - 1;;) {
            while (j >= 0 && !b[j]) j--;
            if (j == -1) break;
            while (i < n && (i + j < n - 1 || (!a[i]))) i++;
            if (i == n) break;
            tmp = min(a[i], b[j]);
            a[i] -= tmp;
            b[j] -= tmp;
            ans += tmp;
            if (i + j >= n) flag = true;
        }
        if (!flag) {
            for (int i = mn + 1; i < n; ++i) {
                if (a[i]) flag = 1;
            }
        }
        if (!flag) ans--;
        cout << ans << "\n";
    }
    return 0;
}
/*
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

1
10
5 3 5 3 2 4 2 4 1 5
9 9 8 2 4 4 3 5 3 0
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5636kb

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
Wrong Answer
time: 51ms
memory: 5652kb

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
4
0
4
1
0
2
3
3
1
5
0
0
2
0
0
1
1
0
0
3
5
3
2
2
1
0
1
2
2
2
0
3
0
2
1
1
0
1
0
4
0
0
2
2
0
3
3
0
2
0
1
0
0
1
1
2
0
3
4
0
2
5
0
2
1
0
0
0
3
2
3
0
2
0
4
3
3
0
2
2
0
1
3
1
1
0
0
0
1
0
3
2
2
0
2
0
1
0
2
0
0
2
4
1
2
3
2
2
2
0
2
0
0
2
3
1
3
1
0
2
2
3
0
1
2
0
1
1
...

result:

wrong answer 47th numbers differ - expected: '2', found: '1'