题目链接: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;
}

Codeforces 540B School Marks 【贪心构造】相关推荐

  1. CodeForces - 540B School Marks —— 贪心

    题目链接:https://vjudge.net/contest/226823#problem/B Little Vova studies programming in an elite school. ...

  2. CodeForces - 1265D Beautiful Sequence(贪心+构造+思维)

    题目链接:点击查看 题目大意:给出a个0,b个1,c个2,d个3,要求构造一种序列,使得数列两两之间绝对值之差等于1,若不能构造输出NO 题目分析:首先我们需要稍微讨论一下特殊情况,那就是对于两端的数 ...

  3. CodeForces - 1256C Platforms Jumping(贪心+构造)

    题目链接:点击查看 题目大意:现在固定人初始时在点0处,现在我们需要跨过长度为n的一条河,到达对岸的点n+1处,给出m个木板,我们可以将这m个木板随意摆放,但相对位置不能改变,并且只能互相接触而不能互 ...

  4. CodeForces - 1255D Feeding Chicken(贪心+构造+模拟)

    题目链接:点击查看 题目大意:给出一个n*m的农场,其中'.'代表空地,'R'代表大米,现在有k只鸡需要分布在这个农场之中,需要满足以下条件: 每个方格都要被鸡占领 每只鸡至少占领一个方格 每只鸡占领 ...

  5. 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation

    题目传送门 1 /* 2 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 3 */ 4 /**************************************** ...

  6. 2021年度训练联盟热身训练赛第五场 H题In-place Sorting+贪心构造

    题意: 给你n个小于101810^{18}1018的大数,问在可以再不改变序列位置,之改变数值中某数位的'9'变为'6'或将'6'变为'9',求的最终序列由小到大,且字典序最小. 题目: 链接:htt ...

  7. 【牛客 - 318G】LLLYYY的数字思维 与【牛客 - 289J】这是一个沙雕题II(贪心构造)

    题干: LLLYYY很喜欢写暴力模拟贪心思维.某一天在机房,他突然抛给了队友ppq一 个问题.问题如下: 有一个函数f (): int f(int x){     int tmp = 0;     w ...

  8. 常规贪心构造题 最多能完成排序的块 II

    这是 LeetCode 上的  最多能完成排序的块 II ,难度为 困难. Tag : 「贪心」 这个问题和"最多能完成排序的块"相似,但给定数组中的元素可以重复,输入数组最大长度 ...

  9. CodeForces - 1494D Dogeforces(贪心+构造)

    题目链接:点击查看 题目大意:给出 nnn 个叶子结点和一个 n∗nn*nn∗n 的 LCALCALCA 矩阵,其中 LCALCALCA 表示的是最近公共祖先节点的权值,现在需要构造出一棵自顶向下权值 ...

最新文章

  1. scheduled 一秒钟执行一次_spring boot的Scheduled帮你实现定时任务,spring boot实践(11)...
  2. MIPS介绍与MIPS汇编的常用指令
  3. 推荐一个采用方便程序员在线动画学习常用算法的良心网站
  4. 输出等边三角形php,php打印三角星星方法实列
  5. UserThreadLocal 用户线程Token拦截验证
  6. mongodb 查询内嵌文档
  7. 2.3.12 Python 函数进阶-装饰器
  8. ubuntu 网络配置
  9. ZDI 公布2020年 Pwn2Own 东京赛规则和奖金
  10. excel学习range
  11. 人事管理系统(毕业设计)
  12. 基于热传导方程的高温作业专用服装设计(三)
  13. 诛仙服务器 修改技能伤害,《诛仙3》技能调整优化【 技能修改·天脉】
  14. 使用snap安装microk8s
  15. 重装Ubuntu系统
  16. Leetcode 408: Valid Word Abbreviation
  17. 股票分红信息 股票历次分红查询
  18. 孩子为什么不愿意再跟家长沟通?家长该怎么办
  19. sip是什么?Mac电脑如何关闭sip?关闭系统完整性保护SIP的方法教程
  20. 电话卡插到终端服务器通话时长,如何降低呼叫中心通话时长而不影响服务质量...

热门文章

  1. 分享 7 个 AI 优质开源项目!文本生成、自动化数据搜集...
  2. 收藏~六大网站访客类型及应对方式
  3. 人工智能到底是风口还是泡沫?它能否改变未来“钱景”
  4. Q_INIT_RESOURCE宏
  5. 为什么高低温环境试验箱不能满载做试验
  6. PMCAFF:揭秘暗藏在O2O背后的暴利刷单,1800元投入刷单2个月赚50万
  7. 获取iframe实际高度
  8. Linux下安装android手机ADB驱动
  9. 先根,中根,后根遍历
  10. 中国人民公安大学网络对抗技术实验三