CodeForces - 540B School Marks —— 贪心
题目链接:https://vjudge.net/contest/226823#problem/B
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 ypoints (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, nis 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
5 3 5 18 4
3 5 4
4 1
5 3 5 16 4
5 5 5
-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个数,已知其中k个,且所有数的大小都不超过p且不小于1。问是否有这样一组数,满足:他们的和不超过x,中位数不小于y?
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+; int n, k, p, x, y, a[MAXN];
int main()
{
while(cin>>n>>k>>p>>x>>y)
{
int left = x;
for(int i = ; i<=k; i++)
scanf("%d",&a[i]), left -= a[i]; sort(a+,a++k); //对已有的数进行排序
int mid = (n+)/; //中位数的下标
int pos = lower_bound(a+,a++k,y)-(a+); //求出比y小的最大的数的下标(亦是个数)
if(pos>=mid) //如果下标达到或超过了中位数的下标,则必定满足不了条件
left = -;
else //没有达到中位数的下标,则继续
{
int cnt = mid--pos; //为了尽可能保留left,在中位数之前的剩余空位全部放1
for(int i = k+; i<=k+cnt&&i<=n; i++)
a[i] = , left -= ;
for(int i = k+cnt+; i<=n; i++) //中位数以及剩余的为全部放y。
a[i] = y, left -= y;
} if(left<)
puts("-1");
else
{
for(int i = k+; i<=n; i++)
printf("%d ", a[i]);
puts("");
}
}
}
CodeForces - 540B School Marks —— 贪心的更多相关文章
- (CodeForces )540B School Marks 贪心 (中位数)
Little Vova studies programming to p. Vova is very smart and he can write every test for any mark, b ...
- CodeForces 540B School Marks
http://codeforces.com/problemset/problem/540/B School Marks Time Limit:2000MS Memory Limit:26214 ...
- CodeForces 540B School Marks (贪心)
题意:先给定5个数,n, k, p, x, y.分别表示 一共有 n 个成绩,并且已经给定了 k 个,每门成绩 大于0 小于等于p,成绩总和小于等于x, 但中位数大于等于y.让你找出另外的n-k个成 ...
- codeforces 540B.School Marks 解题报告
题目链接:http://codeforces.com/problemset/problem/540/B 题目意思:给出 k 个test的成绩,要凑剩下的 n-k个test的成绩,使得最终的n个test ...
- CodeForces 540B School Marks(思维)
B. School Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- codeforces 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
- CodeForces - 50A Domino piling (贪心+递归)
CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...
- Codeforces 161 B. Discounts (贪心)
题目链接:http://codeforces.com/contest/161/problem/B 题意: 有n个商品和k辆购物车,给出每个商品的价钱c和类别t(1表示凳子,2表示铅笔),如果一辆购物车 ...
- CodeForces 176A Trading Business 贪心
Trading Business 题目连接: http://codeforces.com/problemset/problem/176/A Description To get money for a ...
随机推荐
- sprinvmvc整合swagger实现实时接口信息展示
1.pom.xml引入swagger插件 <dependency> <groupId>io.springfox</groupId> <artifactId&g ...
- PHP 变量定义及使用
php的变量前面必须有$符号,而且是解释型的弱类型语言,定义的时候不需要定义变量值的类型. $str="这是个变量"; 1.输出的时候可以用拼接字符串的方法 如:echo" ...
- centos网络配置实例
1.配置DNS vim /etc/resolv.conf nameserver 192.168.0.1 nameserver 8.8.8.8 nameserver 8.8.4.4 2.配置网关 r ...
- 4种使用webpack提升vue应用的方式
本文参考自:https://mp.weixin.qq.com/s?src=11×tamp=1526886111&ver=889&signature=u9SixhvlJ ...
- 图论 Krusal算法C++实现
1.实验用例 如下图所示的赋权图表示某七个城市及预先算出它们之间的一些直接通信成路造价(单位:万元),试给出一个设计方案,使得各城市之间既能够通信又使总造价最小并计算其最小值. 2实验原理和方法 为了 ...
- 监听iOS检测屏幕旋转状态,不需开启屏幕旋转
-(void)rotation_icon:(float)n { UIButton *history_btn= [self.view viewWithTag:<#(NSInteger)#>] ...
- python实现区块链代码
如果你明白了原理其实挺简单的. 加密算法是python自带的 需要导入hashlib import hashlib as hash sha = hasher.sha256() sha.update(' ...
- 【7.1.1】ELK集群搭建 之 ES集群
写在前边 昨天晚上就已经完成这篇博客了,就是在测试这块是否正常跑起来,晚上没搞完,上班前把电脑关机带着,结果没保存!基本上昨天写的东西都丢了,好在博客园的图片url还在. 为了让大家都轻松些,我轻松写 ...
- Python操作Execl 实现自动化填表
任务简述: 表1是一个简单的数据表,共有110行,25列.第1行是表头,例如“负责人”.“事项”.“期限”等. 第2行——第110行是对应的数据,如“张三”.“搬砖头”.“3天”. 想要做的表(表2) ...
- Koa2+MySQL+VUE+ElementIUI搭建简单的后台管理小系统
如题,前端入坑许久,还是写个小东西出来吧 想要搭建自己的一个后台管理,实现简单的增删改查,看起来很简单 其实是真的简单,没有想的那么难,我也就写了一个月吧, 当然是假的,其实也就每天一两个小时,花了大 ...