Codeforces Round #339 (Div. 2)-D-Skills(二分)

本文介绍了一种游戏角色技能提升的优化算法,通过合理分配资源来最大化玩家角色的力量值,包括技能等级的提升策略及其实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Skills
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai — the current skill level. All skills have the same maximum level A.

Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values:

  • The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf.
  • The minimum skill level among all skills (min ai), multiplied by coefficient cm.

Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force.

Input

The first line of the input contains five space-separated integers nAcfcm and m (1 ≤ n ≤ 100 0001 ≤ A ≤ 1090 ≤ cf, cm ≤ 1000,0 ≤ m ≤ 1015).

The second line contains exactly n integers ai (0 ≤ ai ≤ A), separated by spaces, — the current levels of skills.

Output

On the first line print the maximum value of the Force that the character can achieve using no more than m currency units.

On the second line print n integers a'i (ai ≤ a'i ≤ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces.

Examples
input
3 5 10 1 5
1 3 1
output
12
2 5 2 
input
3 5 10 1 339
1 3 1
output
35
5 5 5 
Note

In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1.

In the second test one should increase all skills to maximum.



这应该是我补的最有毒性的一场cf了。。。 (还好这场没打)

附上惨图:


言归正传,这道题的题意是给你n个技能,每个技能的提升上限时A,然后你有m次
提升技能的机会,每次只能提升一个技能,然后问你这个人的能量最大是多少?
一个人的能量定义为技能为A的个数乘以cf+技能最小值*cm  (cf、 cm题上已经告诉你了)

题解:我们考虑枚举技能能加到A的技能个数,然后二分最小值,这样的话复杂度是n^2logn
还是会TLE的,我的方法是先将所有技能从小到大排序,然后定义前缀和。
该前缀和表示当前技能之前的所有技能加到当前技能所花的操作次数是多少。
这样的话,我们可以考虑二分找当前最小值是否满足题意。也就是二分两次。
总复杂度nlognlogn 。 然而debug了两小时。。。。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
ll n,m,cf,cm,A;
ll a[100005],b[100005],sm1[100005],sm2[100005],as[100005];
bool check(ll x,ll sum,int p)
{
	ll l=1,r=n,t=n,num=0;
	while(l<=r)
	{
		ll mid=(l+r)/2;
		if(a[mid]>x)
			t=mid-1,r=mid-1;
		else
			l=mid+1;
	}
	if(t>=p)
		t=p-1;
	num=num+sm2[t];
	num=num+(x-a[t])*t;
	if(sum-num>=0)
		return 1;
	return 0;
}
int main(void)
{
	ll ans=0,xx=0,yy=0;
	scanf("%lld%lld%lld%lld%lld",&n,&A,&cf,&cm,&m);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]),b[i]=a[i];
	sort(a+1,a+n+1);
	for(int i=n;i>0;i--)
		sm1[i]=sm1[i+1]+A-a[i];
	for(int i=2;i<=n;i++)
		sm2[i]=sm2[i-1]+(a[i]-a[i-1])*(i-1);
	for(int i=0;i<=n;i++)
	{
		ll p=a[n-i+1];
		ll ret=m-sm1[n-i+1];
		if(ret<0) break;
		ll l=0,r=A,tmp=0;
		while(l<=r)
		{
			ll mid=(l+r)/2;
			if(check(mid,ret,n-i+1))
			{
				tmp=mid;
				l=mid+1;
			}
			else
				r=mid-1;
		}
		if(cf*i+cm*tmp>ans)
		{
			ans=cf*i+cm*tmp;
			xx=tmp;yy=p;
		}
	}
	for(int i=1;i<=n;i++)
		as[i]=b[i];
	for(int j=1;j<=n;j++)
	{
		if(b[j]<=xx)
			as[j]=xx;
		if(yy!=0 && b[j]>=yy)
			as[j]=A;
	}
	printf("%lld\n",ans);
	for(int i=1;i<=n;i++)
		printf("%lld ",as[i]);
	printf("\n");
	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值