Codeforces Round #661 (Div. 3)题解ABC

A remove smalles

题意思路:将数组排序 看相邻数子之差是否有大于1的

#include
using namespace std;
//int a[33][33];
int main(){
    int T;
    cin >>T;
    while(T--){
        int n;
        cin >>n;
        int a[n];
        for(int i = 0; i < n; i++)
            cin >> a[i];
        int flag = 0;
        sort(a, a+n);
        for(int i = 1; i< n; i++){
            if(abs(a[i] - a[i-1]) >= 2)
                flag = 1;
        }

        if(flag == 0)
            cout << "YES" <<endl;
        else
            cout << "NO" <<endl;
    }
}

B Gifts Fixing

题意思路: 找出每个数组的最小值 算出每个元素与最小值之差 两个数组中取这个最小值之差大的相加

#include
using namespace std;
#define LL long long
//int a[33][33];
int main(){
    int T;
    cin >>T;
    while(T--){
        int n;
        cin >>n;
        LL a[n];
        LL b[n];
        LL ma[n];
        LL mb[n];
        LL ans[n];
        LL min_b = 0;
        LL min_a = 0;
        LL summ = 0;
        cin >> a[0];
        min_a = a[0];
        for(int i = 1; i < n; i++){
            cin >> a[i];
            if(a[i] < min_a)
                min_a = a[i];
        }

        cin >> b[0];
        min_b = b[0];
        for(int i = 1; i < n; i++){
            cin >> b[i];
            if(b[i] < min_b)
                min_b = b[i];
        }

        for(int i = 0; i < n; i++){
            ma[i] = a[i] - min_a;
            mb[i] = b[i] - min_b;
            ans[i] = max(ma[i], mb[i]);
            summ += ans[i];
        }
        cout << summ << endl;
    }
}

C. Boats Competition

题意:找出一个s 使数组中两个元素相加之和等于s为一组 求使组数最多的s的值
思路:将数组排序 s从a[0]+a[1]到a[n-1]+a[n-1]遍历,任取两个数相加 若等于s 则这两个数赋值100(不在计算)(设置一个数组aa用于每次便利初始化)

#include
using namespace std;
#define LL long long
int main(){
    int T;
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        int ans = 0;
        int summ = 0;
        int a[51];
        int aa[51];

        for(int i = 0; i < n; i++){
            cin >> a[i];
            //cout << '===' << endl;
        }
        sort(a, a+n);

        for(int i = 0; i < n; i++){
            aa[i] = a[i];
        }
        /*for(int i = 0; i < n; i++){
            cout << "a=" << a[i] << ' ';
        }
        cout << endl;
        for(int i = 0; i < n; i++){
            cout << "aa=" << aa[i] << ' ';
        }
        cout << endl;*/
        for(int i = a[0]+a[1]; i <= a[n-2]+a[n-1]; i++){
            //初始化
            ans = 0;
            for(int ii = 0; ii < n; ii++)
                a[ii] = aa[ii];

            for(int j = 0; j < n; j++){
                for(int k = 0; k < n; k++){
                    if(j == k)
                        continue;
                    if(a[k] + a[j] == i){
                        ans++;
                        //cout << "case : " <
                        a[k] = 100;
                        a[j] = 100;
                    }
                }
            }
            if(summ < ans)
                summ = ans;
        }
        cout << summ <<endl;

    }
    return 0;
}

你可能感兴趣的:(cf题解)