Test instructions: First given 5 numbers, n, K, p, X, Y. Respectively, there is a total of n scores, and has been given K, each gate score greater than 0 is less than or equal to P, the sum of scores is less than or equal to X,
But the median is greater than or equal to Y. Let you find another n-k score.
Analysis: The use of greedy algorithm, the first is only less than equal to P, that is, the smaller the better, and then the median number is greater than or equal to Y, so we put a score of only two, one is 1, the other is Y, then the optimal,
So every time as long as the judgment of this is good, in the determination of the time, to first sort, then find the median, and then put two each time, if n-k is not even, then put a number, then proceed.
The code is as follows:
#include <bits/stdc++.h>using namespace Std;typedef long long ll;const int MAXN = 1e5 + 5;const int INF = 0x3f3f3f3f ;vector<int> ans;int A[1005];int Main () {int n, m, p, X, Y, K; scanf ("%d%d%d%d", &n, &k, &p, &x, &y); int sum = 0; for (int i = 0; i < K; ++i) {scanf ("%d", &a[i]); Sum + = A[i]; } int ds = X-sum; int DN = n-k; bool OK = true; if (DN & 1) {sort (A, a+k); if (A[K/2] >= y) {ans.push_back (1); a[k++] = 1; ds--; } else{Ans.push_back (y); a[k++] = y; DS-= y; } DN = n-k; } for (int i = 0; i < dn; i + = 2) {sort (A, a+k); int mid = A[K/2]; if (Mid < Y) {ans.push_back (y); Ans.push_back (y); DS-= y * 2; a[k++] = y; a[k++] = y; } else {a[k++] = 1; Ans.push_back (1); a[k++] = 1; Sort (A, a+k); if (A[K/2] < Y) {a[0] = y; DS-= y + 1; Ans.push_back (y); } else{DS-= 2; Ans.push_back (1); }}} sort (A, a+k); if (A[K/2] < y | | ds < 0) printf ("-1"); else for (int i = 0; i < ans.size (); ++i) if (!i) printf ("%d", ans[i]); else printf ("%d", ans[i]); printf ("\ n"); return 0;}
Codeforces 540B School Marks (greedy)