#301 (div.2) B. School Marks

1.题目描述:点击打开链接

2.解题思路:本题利用贪心法解决。比赛的时候看错了,把y理解成了最小值,花了半天去写一个错误的代码,最后才发现y指的是中位数。。教训颇为惨痛!本题可以先满足中位数y的要求,剩下的都设为1即可。给定了n,那么可知,至多只能有(n-1)/2个数要小于中位数y。因此可以事先统计一下输入的数中有xc1个是小于y的,同时累加和sum。如果sum>x或c1>(n-1)/2,则无解。否则,至少还要添加max(0,(n+1)/2-(k-c1))个中位数。添加完毕之后,由于sum不能超过x,因此只用将剩下的所有数设为1即可。如果最终sum>x,那么无解,否则输出解。

3.代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

#define N 1000+5
int n, k, p, x, y;
vector<int>a;

int main()
{
	//freopen("t.txt", "r", stdin);
	while (~scanf("%d%d%d%d%d", &n, &k, &p, &x, &y))
	{
		int sum = 0;
		a.clear();
		int c1 = 0;
		for (int i = 0; i < k; i++)
		{
			int sx;
			scanf("%d", &sx);
			if (sx < y)c1++;
			sum += sx;
		}
		if (sum>x || c1>(n - 1) / 2)puts("-1");//c1不能超过最大限制(n-1)/2
		else{
			int m = max(0, (n + 1) / 2 - k + c1);//还需要添加的中位数是0和(n+1)-(k-c1)中的较大者
			for (int i = 0; i < m; i++)
				a.push_back(y);
			sum += m*y;
			if (sum + n - k - m>x)puts("-1");
			else{
				for (int i = 0; i < n - k - m; i++)
					a.push_back(1);
				int len = a.size();
				for (int i = 0; i < len; i++)
					printf("%d%c", a[i], i == len - 1 ? '\n' : ' ');
			}
		}
	}
	return 0;
}
时间: 2024-08-22 08:11:42

#301 (div.2) B. School Marks的相关文章

贪心 Codeforces Round #301 (Div. 2) B. School Marks

题目传送门 1 /* 2 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 3 num1是输出的1的个数,numy是除此之外的数都为y,此时的numy是最少需要的,这样才可能中位数大于等于y 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 using na

Codeforces Round #301 (Div. 2) B. School Marks

其实是很水的一道bfs题,昨晚比赛的时候没看清题意,漏了一个条件. 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<cmath> 5 #include<algorithm> 6 #include<set> 7 #include<map> 8 #include<queue> 9 #include<stack>

Codeforces Round #301 (Div. 2) -- (A,B,C,D)

题目传送:Codeforces Round #301 (Div. 2) A. Combination Lock 水题,求最小移动次数,简单贪心一下即可 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #i

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave

题目传送门 1 /* 2 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 3 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两点相邻, 4 那么要不就是踩一脚就破了或者踩一脚走开再走回来踩一脚破了:3. 普通的搜索看是否能到达, 5 若能还是要讨论终点踩几脚的问题:) 6 DFS 耗时大,险些超时,可以用BFS来做 7 */ 8 #include <cstdio> 9 #include <algorit

贪心 Codeforces Round #301 (Div. 2) A. Combination Lock

题目传送门 1 /* 2 贪心水题:累加到目标数字的距离,两头找取最小值 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 using namespace std; 9 10 const int MAXN = 1e3 + 10; 11 const int INF = 0x3f3f3f3f; 12 char s[MAXN],

B. School Marks (CF #301 (Div. 2))

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

「日常训练」School Marks(Codeforces Round 301 Div.2 B)

题意与分析(CodeForces 540B) 代码 #include <iostream> #include <cstring> #include <algorithm> #include <vector> #define MP make_pair #define PB push_back #define fi first #define se second #define ZERO(x) memset((x), 0, sizeof(x)) #define

CodeForces Round #301 Div.2

今天唯一的成果就是把上次几个人一起开房打的那场cf补一下. A. Combination Lock 此等水题看一眼样例加上那个配图我就明白题意了,可是手抽没有注释掉freopen,WA了一发. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 1000 + 10; 5 6 char s1[maxn], s2[maxn]; 7 8 int main() 9 { 10 int n; cin >>