(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道题,已做M题,每题分数a[i],问N-M道题总分不超过S中位数是Y每题分数不超过P;

方法:先求有多少道题分数小于Y,如果ans>N/2 则中位数不是y,否则中位数左边加1右边加y算总和,总和超过s输出-1否则输出1和y。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include <math.h>
#include<queue>
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a));
#define N 51100
using namespace std;
int main()
{
    int n,m,p,y,s,num;
    scanf("%d %d %d %d %d",&n,&m,&p,&s,&y);
        int sum=0,ans=0;
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&num);
            sum+=num;
            if(num<y)
                ans++;
        }
        if(ans<=n/2)///判断中位数是否为y
        {
            int l,r;
            l=min(n/2-ans,n-m);///有几个1
            r=n-l-m;///有几个y
            sum+=l+r*y;
            if(sum>s)
                printf("-1\n");
            else
            {
                for(int i=1;i<=l;i++)
                {
                        printf("%d ",1);

                }

                for(int i=1;i<=r;i++)
                    printf("%d ",y);
                puts("");
            }
        }
        else
            printf("-1\n");
    return 0;
}
时间: 2024-11-04 19:15:01

(CodeForces )540B School Marks 贪心 (中位数)的相关文章

CodeForces 540B School Marks

http://codeforces.com/problemset/problem/540/B School Marks Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 540B Description Little Vova studies programming in an elite school. Vova and

CodeForces 540B School Marks (贪心)

题意:先给定5个数,n,  k, p, x, y.分别表示 一共有 n 个成绩,并且已经给定了 k 个,每门成绩 大于0 小于等于p,成绩总和小于等于x, 但中位数大于等于y.让你找出另外的n-k个成绩. 析:利用贪心算法,首先是只能小于等于 p,也就是成绩越小越好, 然后中位数还得大于等于y,所以我们放的成绩只有两个,一个是1,另一个是y,那么是最优的, 所以每次只要判定这个就好,在判定的时候,要先排序,再找中位数,再就是每次放两个,如果n-k不是偶数,那么就放一个数,再进行. 代码如下: #

cf 540b School Marks 贪心

B. School Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Little Vova studies programming in an elite school. Vova and his classmates are supposed to write n progress tests, for each

Codeforces Round #300——C贪心——Tourist&#39;s Notes

Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, me

Codeforces 77C 树形dp + 贪心

题目链接:点击打开链接 题意: 给定n个点, 每个点的豆子数量 下面是一棵树 再给出起点 每走到一个点,就会把那个点的豆子吃掉一颗. 问:回到起点最多能吃掉多少颗豆子 思路:树形dp 对于当前节点u,先把子节点v都走一次. 然后再往返于(u,v) 之间,直到u点没有豆子或者v点没有豆子. dp[u] 表示u点的最大值.a[u] 是u点剩下的豆子数. #include <cstdio> #include <vector> #include <algorithm> #inc

Codeforces 788A Functions again - 贪心

Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about

Codeforces 486C Palindrome Transformation(贪心)

题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N,指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:其实只要是对称位置不相同的,那么指针肯定要先移动到这里,修改字符只需要考虑两种方向哪种更优即 可.然后将所有需要到达的位置跳出来,贪心处理. #include <cstdio> #include <cstring> #include <cstdlib> #include <vec

codeforces 735C Tennis Championship(贪心+递推)

Tennis Championship 题目链接:http://codeforces.com/problemset/problem/735/C --每天在线,欢迎留言谈论. 题目大意: 给你一个 n (2≤n≤10^18),代表一共有n位参加比赛的选手. 游戏规则: ①每次比赛,输的选手将离开赛场 ②相互比赛的选手 他们的获胜的次数相差不能超过1(获胜4次的选手只能跟3或5次的选手比赛) 问题:最终赢得比赛的选手,胜场最多能为多少. 思路: 贪心:①选一名选手让他一直获胜且优先让他参加比赛 ②当

CodeForces 545C Woodcutters (贪心orDP)

[题目链接]:click here~~ [题目大意]: 有n棵树,给出每棵树的位置和高度,然后把树砍掉,树可以向左倒也可以向右倒.输出最多能砍几棵树. [思路]:利用贪心的思想.第一棵树的左边和最后一棵树的右边没树,所以他们向两边倒,然后对于中间的树来说,首先先向左边倒,然后左边距离如果不够的话再向右边倒,向右倒的时候注意更新一下距离. 代码: /* * Problem: CodeForces 545C * Running time: 46MS * Complier: G++ * Author: