贪心 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 namespace std; 10 11 const int MAXN = 1e3 + 10; 12 const int INF = 0x3f3f3f3f; 13 int a[MAXN]; 14 15 int main(void) //Codeforces Round #301 (Div. 2) B. School Marks 16 { 17 //freopen ("B.in", "r", stdin); 18 19 int n, k, p, x, y; 20 while (scanf ("%d%d%d%d%d", &n, &k, &p, &x, &y) == 5) 21  { 22 int sum = 0, cnt = 0; 23 for (int i=1; i<=k; ++i) 24  { 25 scanf ("%d", &a[i]); sum += a[i]; 26 if (a[i] < y) cnt++; 27  } 28 29 if (cnt <= n / 2) 30  { 31 int num1 = min (n / 2 - cnt, n - k); 32 int numy = n - k - num1; 33 34 sum += num1 + numy * y; 35 if (sum > x) puts ("-1"); 36 else 37  { 38 for (int i=1; i<=num1; ++i) printf ("%d%c", 1, (numy==0 && i==num1) ? '\n' : ' '); 39 for (int i=1; i<=numy; ++i) printf ("%d%c", y, (i==numy) ? '\n' : ' '); 40  } 41  } 42 else puts ("-1"); 43  } 44 45 return 0; 46 }

 

你可能感兴趣的:(codeforces)