C. Splitting Items

_Yxc發表於2024-08-19

https://codeforces.com/contest/2004/problem/C

總結:一開始看錯題了,思維慣性的認為alice會拿前一半大的元素,bob會拿後一半大的元素。。其實不是,而是每個人都挑最大的拿

void solve(){
    int n, k;
    cin >> n >> k;
    
    vector<int> a(n);
    for (auto& x : a) {
        cin >> x;
    }

    sort(a.rbegin(), a.rend());
    for (int i = 1; k && i < n; i += 2) {
        int need = a[i - 1] - a[i];
        if (need <= k) {
            a[i] += need;
            k -= need;
        }
        else {
            a[i] += k;
            k = 0;
        }
    }

    long long ans = 0;
    for (int i = 0; i < n; ++i) {
        ans += (i & 1 ? -1 : 1) * a[i];
    }

    cout << ans << '\n';
}

相關文章