codeforces B. School Marks
来源:互联网 发布:怎样发淘宝买家秀 编辑:程序博客网 时间:2024/06/05 17:04
Little Vova studies programming in an elite school. Vova and his classmates are supposed to writen progress tests, for each test they will get a mark from 1 top. 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 valuex, 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 thany points (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 marksa1, ..., 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.
The first line contains 5 space-separated integers: n,k, p,x and y (1 ≤ n ≤ 999,n is 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.
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.
5 3 5 18 43 5 4
4 1
5 3 5 16 45 5 5
-1
The median of sequence a1, ..., an wheren 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".
WA在15:
#include<iostream>#include<string>#include <stdio.h>#include<algorithm>using namespace std;int main(){ int n,k,p,x,y,sum=0; int a[1002],b[1002]; cin>>n>>k>>p>>x>>y; for(int i=0;i<k;i++) { cin>>a[i]; sum+=a[i]; } int c=x-sum; int flag=0; if(c<n-k){cout<<"-1"<<endl;return 0;} //sort(a,a+k); if(k==0){ for(int i=k;i<n;i++) { if(c>=y+n-i-1) {a[i]=y;c-=y;b[i]=y;} else {b[i]=1;a[i]=1;} } } else { for(int i=k;i<n;i++) { if(c>=y+n-i&&flag<=n/2) {a[i]=y;c-=y;b[i]=y;flag++;} else {b[i]=1;a[i]=1;} } } sort(a,a+n); if(a[(n-1)/2]<y){cout<<"-1"<<endl;return 0;} for(int i=k;i<n-1;i++) cout<<b[i]<<' '; cout<<b[n-1]<<endl; return 0;}方法是一样的,关键是怎么简便的实现
First count the number of marks that are less than y. If there are more than such marks, we can't satisfy the second condition (about the median), and the answer is -1. Otherwise we can get exactly such number of y marks so that the total number of marks greater than or equal toy is at least (maybe it's already satisfied). This is the required action for satisfying the second condition.
Now, in order not to break the first condition, get the remaining marks as lower as possible — all ones — and check the sum of the marks. If it is greater thanx, the answer is -1, otherwise the correct answer is found.
AC:
#include<iostream>#include<string>#include <stdio.h>#include<algorithm>using namespace std;int main(){ int n,k,p,x,y; int a[1002]; cin>>n>>k>>p>>x>>y; int ans=0; int sum=0; for(int i=1;i<=k;i++) { cin>>a[i]; sum+=a[i]; if(a[i]<y) ans++; } int l,r; if(ans<=n/2){ l=min(n/2-ans,n-k); r=n-l-k; sum+=l+r*y; if(sum>x) printf("-1\n"); else{ for(int i=1;i<=l;i++) printf("1 "); for(int i=1;i<=r;i++) printf("%d ",y); printf("\n"); } } else printf("-1\n"); return 0;}
- codeforces B. School Marks
- codeforces B. School Marks
- Codeforces 540B School Marks
- CodeForces 540B School Marks
- codeforces 540B School Marks
- Codeforces 540B - School Marks
- CodeForces 540B School Marks
- School Marks CodeForces - 540B
- Codeforces 540B - School Marks (思维)
- Codeforces 540B - School Marks (贪心)
- [CodeForces 540B]School Marks[贪心]
- Codeforces 540B School Marks 【贪心构造】
- CodeForces - 540B School Marks (数学思维题 中位数)
- Codeforces Round #301 (Div. 2) B. School Marks(贪心)
- CF 301 B School Marks
- Codeforces Round #301 (Div. 2)B. School Marks//CodeForces - 540B 贪心
- B. School Marks (CF #301 (Div. 2))
- cf 540b School Marks 贪心
- JAVA常用类之——System和Runtime
- 漫谈程序员系列:怎么告别“混日子”
- Java设计模式之单例模式k
- Android优化之ViewStub
- node C++扩展(2) 尝试翻译node官网文档
- codeforces B. School Marks
- JAVA图形编程Swing之——JPanel绘图
- 使用指针方式进行数组排序 选择排序:
- Qt上第三方串口类QExtSerialPort的使用
- 数字图像模式识别技术详解【笔记】
- GRBL八:STM32代码移植——串口
- CF 557C(Arthur and Table-直接暴力)
- hdu 5058 So easy
- 括号配对问题