B - School Marks CodeForces - 540B(贪心)

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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值