首页 > 代码库 > CF540B School Marks

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