CF540B School Marks
Posted 王宜鸣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF540B School Marks相关的知识,希望对你有一定的参考价值。
思路:
贪心。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 7 int n, p, k, x, y; 8 vector<int> a; 9 int main() 10 { 11 cin >> n >> k >> p >> x >> y; 12 int sum = 0, tmp = 0; 13 for (int i = 0; i < k; i++) 14 { 15 cin >> tmp; 16 a.push_back(tmp); 17 sum += a[i]; 18 } 19 if (x - sum < n - k) 20 { 21 puts("-1"); 22 } 23 else 24 { 25 bool flag = true; 26 vector<int> res; 27 sort(a.begin(), a.begin() + k); 28 int now = k; 29 sum = x - sum; 30 while (now < n) 31 { 32 int pos = lower_bound(a.begin(), a.begin() + now, y) - a.begin(); 33 if (pos < now / 2) 34 { 35 a.insert(a.begin(), 1); 36 res.push_back(1); 37 sum--; 38 } 39 else 40 { 41 a.insert(a.begin() + pos, y); 42 res.push_back(y); 43 sum -= y; 44 } 45 if (sum < 0) 46 { 47 puts("-1"); 48 flag = false; 49 break; 50 } 51 now++; 52 } 53 if (flag) 54 { 55 if (a[n >> 1] >= y) 56 { 57 for (int i = 0; i < n - k; i++) 58 { 59 cout << res[i] << " "; 60 } 61 puts(""); 62 } 63 else 64 { 65 puts("-1"); 66 } 67 } 68 } 69 return 0; 70 }
以上是关于CF540B School Marks的主要内容,如果未能解决你的问题,请参考以下文章
CF #404 (Div. 2) D. Anton and School - 2 (数论+范德蒙恒等式)