Codeforces 540B School Marks 【贪心构造】

题目链接:Codeforces 540B School Marks

Little Vova studies programming in an elite school. Vova and his classmates are supposed to write n progress tests, for each test they will get a mark from 1 to p. Vova is very smart and he can write every test for any mark, but he doesn’t want to stand out from the crowd too much. If the sum of his marks for all tests exceeds value x, then his classmates notice how smart he is and start distracting him asking to let them copy his homework. And if the median of his marks will be lower than y points (the definition of a median is given in the notes), then his mom will decide that he gets too many bad marks and forbid him to play computer games.

Vova has already wrote k tests and got marks a1, …, ak. He doesn’t want to get into the first or the second situation described above and now he needs to determine which marks he needs to get for the remaining tests. Help him do that.

Input
The first line contains 5 space-separated integers: n, k, p, x and y (1 ≤ n ≤ 999, n is odd, 0 ≤ k < n, 1 ≤ p ≤ 1000, n ≤ x ≤ n·p, 1 ≤ y ≤ p). Here n is the number of tests that Vova is planned to write, k is the number of tests he has already written, p is the maximum possible mark for a test, x is the maximum total number of points so that the classmates don’t yet disturb Vova, y is the minimum median point so that mom still lets him play computer games.

The second line contains k space-separated integers: a1, …, ak (1 ≤ ai ≤ p) — the marks that Vova got for the tests he has already written.

Output
If Vova cannot achieve the desired result, print “-1”.

Otherwise, print n - k space-separated integers — the marks that Vova should get for the remaining tests. If there are multiple possible solutions, print any of them.

Examples
input
5 3 5 18 4
3 5 4
output
4 1
input
5 3 5 16 4
5 5 5
output
-1
Note
The median of sequence a1, …, an where n is odd (in this problem n is always odd) is the element staying on (n + 1) / 2 position in the sorted list of ai.

In the first sample the sum of marks equals 3 + 5 + 4 + 4 + 1 = 17, what doesn’t exceed 18, that means that Vova won’t be disturbed by his classmates. And the median point of the sequence {1, 3, 4, 4, 5} equals to 4, that isn’t less than 4, so his mom lets him play computer games.

Please note that you do not have to maximize the sum of marks or the median mark. Any of the answers: “4 2”, “2 4”, “5 1”, “1 5”, “4 1”, “1 4” for the first test is correct.

In the second sample Vova got three ‘5’ marks, so even if he gets two ‘1’ marks, the sum of marks will be 17, that is more than the required value of 16. So, the answer to this test is “-1”. ##

题意:有一个n个元素的序列(n为奇数),现在给出其中的k个元素,让你构造其它的n-k个元素使得该序列的中位数>=y、序列总和<=x、最大元素<=p。输出任意一种可能即可。

思路:由于中位数的区间很小,我们可以去枚举。对于当前的中位数i,我们求出小于i的元素个数l,大于i的元素个数r,等于i的元素个数cnt。除去中位数自身,对于cnt-1(前提cnt!=0),我们贪心的处理,尽可能的放在i的右边,下面就是填数使左右区间元素个数相等。显然左边填1,右边填i就可以保证序列和尽可能小。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
const int MAXN = 1e3+10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
int a[MAXN], b[MAXN];
int num = 0;
bool judge(int n, int p, int k, int x)
{
    int l = 0, r = 0, cnt = 0;
    int sum = 0;
    for(int i = 1; i <= k; i++) {
        sum += a[i];
        if(a[i] < p) l++;
        else if(a[i] > p) r++;
        else cnt++;
    }
    num = 0;
    if(cnt == 0) b[num++] = p;
    else cnt--;
    if(l > n/2 || r > n/2) return false;
    int yu = n/2 - r;
    r += min(yu, cnt); cnt -= min(yu, cnt);
    l += cnt;
    while(l < r)
    {
        b[num++] = 1;
        l++;
    }
    while(r < l)
    {
        b[num++] = p;
        r++;
    }
    if(l > n/2 || r > n/2) return false;
    for(int i = 1; i <= n/2-l; i++)
        b[num++] = 1, b[num++] = p;
    for(int i = 0; i < num; i++)
        sum += b[i];
    return sum <= x;
}
int main()
{
    int n, k, p, x, y; cin >> n >> k >> p >> x >> y;
    for(int i = 1; i <= k; i++) cin >> a[i];
    bool flag = false;
    for(int i = y; i <= p; i++)
    {
        if(judge(n, i, k, x))
        {
            flag = true;
            break;
        }
    }
    if(!flag)
        cout << -1 << endl;
    else
    {
        for(int i = 0; i < num; i++) {
            if(i) cout << " ";
            cout << b[i];
        }
        cout << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值