题解 | #数的选择#

数的选择

https://www.nowcoder.com/practice/12f72e06fc424e4d9c685cd89f2bed36

#include <bits/stdc++.h> 

using namespace std;

// A集合中尽量取最大的数!
// B集合中尽量取最小的数!

int main(){
    int n;
    cin >> n;
    vector<long long> arr(n);
    for(int i = 0; i < n; i++){
        cin >> arr[i];
    }

    sort(arr.begin(),arr.end());    
    vector<long long> pre_sum{0},suffix_sum{0};

    for(int i = 0; i < n; i++){
        pre_sum.push_back(pre_sum.back() + arr[i]);
    }
    for(int i = n-1; i >= 0; i--){
        suffix_sum.push_back(suffix_sum.back() + arr[i]);
    }   

    // 枚举K
    int ans = -1;
    for(int k = 1; 2*k+1 <= n; k++){

        long long blue_sum = pre_sum[k+1];
        long long red_sum = suffix_sum[k];
        if(blue_sum < red_sum){
            ans = k;
            break;
        }
    }

    cout << ans << endl;
    return 0;
}

全部评论

相关推荐

野猪不是猪🐗:把你的学校加黑,加粗,斜体,下划线,描边,内阴影,内发光,投影,外发光,再上渐变色,居中,放大到最大字号,再把简历里其它内容删了,就行了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务