这道题有人会吗?
查看原帖
这道题有人会吗?
1624467
wu_ming_ren_shi_楼主2025/7/29 21:59

我的代码如下

#include <bits/stdc++.h>
using namespace std;
long long a[500010];
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    sort(a,a+n,greater<long long>());
    long long total=0;
    vector<bool> used(n,false);
    int count=0;
    for(int i=0;i<n && count<n;){
        while(i<n && used[i]) i++;
        if(i>=n) break;
        total += a[i];
        used[i] = true;
        count++;
        i++;
        while(i<n && used[i]) i++;
        if(i>=n) break;
        total += a[i];
        used[i] = true;
        count++;
        long long p = a[i];
        i++;
        int free_idx = -1;
        for(int j=i;j<n;j++){
            if(!used[j] && a[j] <= p/2){
                free_idx = j;
                break;
            }
        }
        if(free_idx != -1){
            used[free_idx] = true;
            count++;
        }
    }
    cout<<total;
    return 0;
}

运行后未知错误,有大佬会的看一下。

压根没题解

只能问一下有人会吗?

2025/7/29 21:59
加载中...