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 ...
随机推荐
- 为什么要点两下才能删除一个li节点 原来是空白节点作怪
奇怪吧,下面的代码居然要点两次button才能删除一个li节点: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional// ...
- C#比較对象的相等性
对于相等的机制全部不同,这取决于比較的是引用类型还是值类型.以下分别介绍引用类型和值类型的相等性. 1.比較引用类型的相等性 System.Object定义了三种不同的方法,来比較对象的相等性:Ref ...
- 云数据库 RDS 版怎么创建数据库和账号MySQL 5.7版
若要使用云数据库RDS,您需要在实例中创建数据库和账号.对于MySQL 5.7版本的实例,您需要通过RDS控制台创建一个初始账号,然后可以通过数据管理(DMS)控制台创建和管理数据库.本文将主要介绍在 ...
- SAS中的自动变量
Sas自动变量:由数据步语句自动创建的. _n_ :观测序号: _error_:错误信息变量; _numeric_ :所有数值变量: _character_:所有字符变量; _all_:所有变量; f ...
- .NET EF 框架-实现增删改查
声明一个EF上下文对象 Model dbContext=new Model(); 添加操作(向表中插入一条数据) //声明一个表的实体 Contact contact =new Contact(); ...
- JavaScript 文件操作方法详解
可以通过浏览器在访问者的硬盘上创建文件,因为我开始试了一下真的可以,不信你把下面这段代码COPY到一个HTML文件当中再运行一下! <script language="JavaScri ...
- Selenium详解
自动化测试工具,支持多种浏览器.爬虫中主要用来解决JavaScript渲染的问题. 主要是操控流量器,让浏览器做一些点击啊.加载渲染js啊,之类的.
- VMware厚置备延迟置零,厚置备置零,精简置备具体解释
本文具体介绍VMware厚置备延迟置零,厚置备置零,精简置备的概念及选择使用 1.厚置备延迟置零(zeroed thick) 以默认的厚格式创建虚拟磁盘.创建过程中为虚拟磁盘分配所需空间.创建时不会擦 ...
- spring boot 发布成包所需插件
在pom.xml里配置 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId> ...
- 【Scala】Scala的Predef对象
隐式引用(Implicit Import) Scala会自己主动为每一个程序加上几个隐式引用,就像Java程序会自己主动加上java.lang包一样. Scala中.下面三个包的内容会隐式引用到每一个 ...