QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#568696#8057. Best Carry Player 4gates_orzTL 7ms22752kbC++172.8kb2024-09-16 17:48:452024-09-16 17:48:45

Judging History

你现在查看的是最新测评结果

  • [2024-09-16 17:48:45]
  • 评测
  • 测评结果:TL
  • 用时:7ms
  • 内存:22752kb
  • [2024-09-16 17:48:45]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
#define int LL
#define double LD
#define endl "\n"
typedef pair<int, int> PII;
const int N = 1e6 + 5;
const int INF = 0x3f3f3f3f;
const int inf = 0x3f3f3f3f3f3f3f3f;
const double PI = acosl(-1.0);
const double eps = 1e-8;
const int MOD = 1e9 + 7;

int a[N], b[N];
PII p[N];
int m;

int esum[N];

inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        x = x * 10 + ch - '0', ch = getchar();
    return x * f;
}

inline void write(int x) {
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
    return;
}
inline int max(int a,int b)
{
    return a>b?a:b;
}
void solve()
{
    m=read();
    int suma = 0, sumb = 0;
    for (int i = 0; i < m; i++) a[i]=read(), suma += a[i];
    for (int i = 0; i < m; i++) b[i]=read(), sumb += b[i];
    if (sumb > suma) swap(a, b);
    int n = 0;
    for (int i = 0; i < m; ++i) {
        if (b[i]) p[++n] = {b[i], i};
    }
    int ans = 0;
    int ca = -1, cb = -1, ssum = inf;
    for (int i = 1; i < m; ++i) {
        if (!a[i]) continue;
        int l = 1, r = n, tmp = r;
        while (l <= r) {
            int mid = (l + r) / 2;
            if (p[mid].second + i >= m) {
                r = mid - 1;
                tmp = mid;
            }
            else {
                l = mid + 1;
            }
        }
        if (ssum > i + p[tmp].second) {
            ca = i;
            cb = p[tmp].second;
            ssum = i + p[tmp].second;
        }
    }
    if (ca != -1 && cb != -1) {
//        cout << ca << " " << cb << " " << endl;
        --a[ca];
        --b[cb];
        ans++;
    }
    else {
        puts("0");
        return;
    }
//    for (int i = 0; i < m; i++) cout << a[i] << endl;
    int cnt = 0;
    for (int i = m - 1; i >= 0; i--) {
        esum[i] = b[i] + esum[i + 1];
    }
//    for (int i = 0; i < m; i++) cout << esum[i] << " ";
    for (int i = 0; i < m; i++) {
        if (a[i] >= esum[max(m - i - 1, 0LL)] - cnt) {
            ans += esum[max(m - i - 1, 0LL)] - cnt;
            a[i] -= esum[max(m - i - 1, 0LL)] - cnt;
            cnt = esum[max(m - i - 1, 0LL)];
        }
        else {
            ans += a[i];
            cnt += a[i];
            a[i] = 0;
        }
    }
    ans += a[m - 1];
//    cout << "olahfc\n";
    //printf("%lld\n", ans);
    write(ans);
    puts("");
}

signed main()
{
//    ios::sync_with_stdio(false), cin.tie(0);
    int _;
    scanf("%lld", &_);
//    _ = 1;
    while (_--) {
        solve();
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 7ms
memory: 22752kb

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

result: