C. Splitting Items

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';
}
posted @ 2024-08-19 14:07  _Yxc  阅读(44)  评论(0编辑  收藏  举报