Educational Codeforces Round 102 (Rated for Div. 2) A. Replacing Elements

A. Replacing Elements A. Replacing Elements

题目分析

题意:给你一个长度为n的数组和一个整数d,允许你让任意一个元素变为另外两个元素的和,问你最后能否让数组的所有元素都小于d

先判断数组中的所有元素是否小于d,如果不小于d,再排个序,判断最小的两个元素的和是否小于2 * d

AC代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int q[N];
int n, d, t;
int main(){
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> t;
    while(t--){
    bool flag = 1;
        cin >> n >> d;
        for(int w = 0; w < n; w++){
            cin >> q[w];
            if(q[w] > d) flag = 0;
        }
        if(flag){
            cout << "yes" << endl; continue;
        }
        sort(q, q + n);
        if(q[0] + q[1] <= d){
            cout << "yes" << endl;
        }
        else{
            cout << "no" << endl;
        }
    }
    
    
    return 0;
}
原文地址:https://www.cnblogs.com/FrankOu/p/14280193.html