题解 | 小红的整数配对

小红的整数配对

https://www.nowcoder.com/practice/66b9810e4fe34956a8d1f5c67aacc6dc

#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main(){
    int n,k;
    cin >> n >>k;
    int score = 0;
    vector<int> a(n);
    for(auto &x : a) cin >> x;
    sort(a.begin(),a.end());
    int i = n -1;
    while(i >= 1){
        if(a[i] - a[i - 1] <=k){
            score+= a[i]*a[i - 1];
            i-=2;
        } else{
            i--;
        }

    }
    cout << score << endl;
    return 0;

}

全部评论

相关推荐

狄文君:多段项目经历 + 专业技能拉满,这简历很能打,坐等好消息!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务