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 y 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.
B - School Marks
贪心。总分要尽可能小,并且要满足中位数的要求,所以就用y作为最小的中位数,固定中位数后,就在其前后补数让中位数真的满足条件,每次补1或者y。
简洁版本:
注意即使补数也绝对不能超过 n
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n,k,p,x,y;
int mid;
while(cin>>n>>k>>p>>x>>y){
int tmp=0,a,sum=0;
for(int i=0;i<k;i++){
cin>>a;
tmp+=a;
if(a<y) sum++;
}
if(sum>n/2) cout<<"-1"<<endl;
else{
int t1=min(n-k,n/2-sum);
int t2=(n-k-t1)*y;
if(t1+t2+tmp>x) cout<<"-1"<<endl;
else{
for(int i=0;i<t1;i++) cout<<"1 ";
for(int i=0;i<t2/y;i++) cout<<y<<" ";
cout<<endl;
}
}
}
}
复杂:
#include <iostream>
using namespace std;
int main()
{
int n,k,p,x,y;
int temp,total=0;
int sum=0;
int a[1005];
int one,two;
cin>>n>>k>>p>>x>>y;
int k1=n-k;
while(k--)
{
cin>>temp;
total+=temp;
if(temp<y)
sum++;
}
if(sum>(n/2))
{
cout<<-1<<endl;
return 0;
}
else
{
one=two=0;
while(k1--)
{
if(sum<(n/2))
{
one++;
sum++;
total++;
}
else
{
two++;
total+=y;
}
}
}
if(total<=x)
{
while(one--)
{
if(one>0)
cout<<1<<" ";
else
cout<<1;
}
if(two>0)
cout<<" ";
while(two--)
{
if(two>0)
cout<<y<<" ";
else
cout<<y;
}
}
else
cout<<-1<<endl;
return 0;
}