求助做法哪里假了
查看原帖
求助做法哪里假了
974277
水星湖psgqwq楼主2024/7/26 10:32
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
const int N = 4e5 + 5;
int n, a[N], f[N];
priority_queue<pair<int, int>> l, r;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    for(int i = 1; i <= 2 * n; i++) cin >> a[i];
    int ans = 0, p1 = n, p2 = p1 + 1;
    for(int i = 1; i <= p1; i++) l.push({a[i], i});
    for(int i = p2; i <= 2 * n; i++) r.push({a[i], i});
    for(int i = 1; i <= n; i++) {
        if(a[p1] < a[p2]) {
            while(f[r.top().second]) r.pop();
            ans += r.top().first;
            if(r.top().second == p2) p2++;
            r.pop();
            f[p1--] = 1;
        }
        else {
            while(f[l.top().second]) l.pop();
            ans += l.top().first;
            if(l.top().second == p1) p1--;
            l.pop();
            f[p2++] = 1;
        }
    }
    cout << ans;
    return 0;
}

AC * 11, WA * 12

2024/7/26 10:32
加载中...