首页 > 其他分享 >C. Splitting Items

C. Splitting Items

时间:2024-08-19 14:04:14浏览次数:10  
标签:int Items 元素 Splitting cin need

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';
}

标签:int,Items,元素,Splitting,cin,need
From: https://www.cnblogs.com/yxcblogs/p/18367190

相关文章