CodeForces - 540B School Marks —— 贪心
題目鏈接:https://vjudge.net/contest/226823#problem/B
?
Little Vova studies programming in an elite school. Vova and his classmates are supposed to write?n?progress tests, for each test they will get a mark from 1 to?p. Vova is very smart and he can write every test for any mark, but he doesn't want to stand out from the crowd too much. If the sum of his marks for all tests exceeds value?x, then his classmates notice how smart he is and start distracting him asking to let them copy his homework. And if the median of his marks will be lower than?ypoints?(the definition of a median is given in the notes), then his mom will decide that he gets too many bad marks and forbid him to play computer games.
Vova has already wrote?k?tests and got marks?a1,?...,?ak. He doesn't want to get into the first or the second situation described above and now he needs to determine which marks he needs to get for the remaining tests. Help him do that.
Input
The first line contains 5 space-separated integers:?n,?k,?p,?x?and?y?(1?≤?n?≤?999,?nis odd,?0?≤?k?<?n,?1?≤?p?≤?1000,?n?≤?x?≤?n·p,?1?≤?y?≤?p). Here?n?is the number of tests that Vova is planned to write,?k?is the number of tests he has already written,?p?is the maximum possible mark for a test,?x?is the maximum total number of points so that the classmates don't yet disturb Vova,?y?is the minimum median point so that mom still lets him play computer games.
The second line contains?k?space-separated integers:?a1,?...,?ak?(1?≤?ai?≤?p)?— the marks that Vova got for the tests he has already written.
Output
If Vova cannot achieve the desired result, print "-1".
Otherwise, print?n?-?k?space-separated integers?— the marks that Vova should get for the remaining tests. If there are multiple possible solutions, print any of them.
Examples
Input 5 3 5 18 43 5 4 Output 4 1 Input 5 3 5 16 4
5 5 5 Output -1
Note
The median of sequence?a1,?...,?an?where?n?is odd (in this problem?n?is always odd) is the element staying on?(n?+?1)?/?2?position in the sorted list of?ai.
In the first sample the sum of marks equals 3 + 5 + 4 + 4 + 1 = 17, what doesn't exceed 18, that means that Vova won't be disturbed by his classmates. And the median point of the sequence {1, 3, 4, 4, 5} equals to 4, that isn't less than 4, so his mom lets him play computer games.
Please note that you do not have to maximize the sum of marks or the median mark. Any of the answers: "4?2", "2?4", "5?1", "1?5", "4?1", "1?4" for the first test is correct.
In the second sample Vova got three '5' marks, so even if he gets two '1' marks, the sum of marks will be 17, that is more than the required value of 16. So, the answer to this test is "-1".
?
題意:
有n個數,已知其中k個,且所有數的大小都不超過p且不小于1。問是否有這樣一組數,滿足:他們的和不超過x,中位數不小于y?
?
代碼如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <cstdlib> 6 #include <string> 7 #include <vector> 8 #include <map> 9 #include <set> 10 #include <queue> 11 #include <sstream> 12 #include <algorithm> 13 using namespace std; 14 typedef long long LL; 15 const double eps = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 9e18; 18 const int MOD = 1e9+7; 19 const int MAXN = 1e5+10; 20 21 int n, k, p, x, y, a[MAXN]; 22 int main() 23 { 24 while(cin>>n>>k>>p>>x>>y) 25 { 26 int left = x; 27 for(int i = 1; i<=k; i++) 28 scanf("%d",&a[i]), left -= a[i]; 29 30 sort(a+1,a+1+k); //對已有的數進行排序 31 int mid = (n+1)/2; //中位數的下標 32 int pos = lower_bound(a+1,a+1+k,y)-(a+1); //求出比y小的最大的數的下標(亦是個數) 33 if(pos>=mid) //如果下標達到或超過了中位數的下標,則必定滿足不了條件 34 left = -1; 35 else //沒有達到中位數的下標,則繼續 36 { 37 int cnt = mid-1-pos; //為了盡可能保留left,在中位數之前的剩余空位全部放1 38 for(int i = k+1; i<=k+cnt&&i<=n; i++) 39 a[i] = 1, left -= 1; 40 for(int i = k+cnt+1; i<=n; i++) //中位數以及剩余的為全部放y。 41 a[i] = y, left -= y; 42 } 43 44 if(left<0) 45 puts("-1"); 46 else 47 { 48 for(int i = k+1; i<=n; i++) 49 printf("%d ", a[i]); 50 puts(""); 51 } 52 } 53 } View Code?
轉載于:https://www.cnblogs.com/DOLFAMINGO/p/8995349.html
總結
以上是生活随笔為你收集整理的CodeForces - 540B School Marks —— 贪心的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转]springmvc常用注解标签详解
- 下一篇: Linux(DeepInOS) 下 my