3664. 数组补全

3664. 数组补全

题目链接
3664. 数组补全
3664. 数组补全

//我的dd代码
#include <bits/stdc++.h>
using namespace std;

int n, k, p, x, y;
int a[1100];
int sum;
int k1, k2;
int main() {
    cin >> n >> k >> p >> x >> y;
    for (int i = 0; i < k; ++i) {
        cin >> a[i];
        sum += a[i];
        if (a[i] >= y)
            k1++;
        else
            k2++;
    }
    
    for (int i = k; i < n; ++i) {
        if (k1 <= k2) {
            sum += y;
            a[i] = y;
            k1++;
        } else {
            sum += 1;
            a[i] = 1;
            k2++;
        }
    }
    if (sum <= x && k1 > k2) {
        for (int i = k; i < n; ++i) {
            cout << a[i] << " ";
        }
        cout << endl;
    } else
        cout << "-1" << endl;
    return 0;
}
//y总的dl代码
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, k, p, x, y;
    cin >> n >> k >> p >> x >> y;
    int sum = 0, lt = 0, ge = 0;
    for (int i = 0; i < k;++i){
        int t;
        cin >> t;
        sum += t;
        if(t<y)
            lt++;
        else
            ge++;
    }
    int l = n / 2, r = n / 2 + 1;
    if(lt>1||ge>r)
        puts("-1");
    else{
        sum += (l - lt) * 1 + (r - ge) * y;
        if(sum>x)
            puts("-1");
        else{
            for (int i = 0; i < 1 - lt;++i)
                cout << 1 << ' ';
            for (int i = 0; i < r - ge;++i)
                cout << y << ' ';
        }
    }
    return 0;
}

题解:为了维持中位数是大于等于y而且总和不大于x,能加进来的数要么就是1要么就是y(这样可以保证x最小),所以只需要比较大于等于y的个数和小于y的个数就可以了

上一篇:阿里云acp认证有用吗 阿里云acp认证考试报名方法介绍


下一篇:4.选举