Codeforces Round #339 (Div. 2)D. Skills

D. Skills
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 n, A, cf, cm and m (1 ≤ n ≤ 100 000, 1 ≤ A ≤ 109, 0 ≤ 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.

Sample test(s)
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.

记最小值ansmin,最小值个数ansi, perfect个数per_cnt;
ans=per_cnt*cf+ansmin*cm;
可以想到枚举perfect个数,剩余价值用来提高最小值ansmin.

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<math.h>
#include<string.h>
using namespace std;
#define LL __int64
#define N 100005
struct node
{
    int val,id;
}a[N];
LL s[N];
bool cmp1(node a,node b)
{
    return a.val<b.val;
}
bool cmp2(node a,node b) 
{
    return a.id<b.id;
}

int main()
{
    int i,j,n,A,cf,cm;
    LL m;
    while(~scanf("%d%d%d%d%I64d",&n,&A,&cf,&cm,&m)){
        for(i=0;i<n;++i){
            scanf("%d",&a[i].val);
            a[i].id=i;
        }
        sort(a,a+n,cmp1);     //按值大小排序,非递减
        s[0]=0;           //初始化累加和
        for(i=0;i<n;++i){
            s[i+1]=s[i]+a[i].val;
        }
        LL ans=-1,ansi,ansj,ansmin;
        LL cost,tmp,tmin,left;
        for(i=0,j=0;i<=n;++i){     //有几个值改变为perfect,i取值范围[0,n]
            cost=(LL)A*(n-i)-(s[n]-s[i]);
            if(cost>m)
                continue;
            tmp=(n-i)*cf;
            left=m-cost;
            while(j<i&&(LL)a[j].val*j-s[j]<=left){
                ++j;   //判断{a[0].val,...,a[j-1].val}这j个值加上剩余价值left,能否达到a[j].val
            }           //如果可以达到,则{0,...,j-1,j},共(j+1)个值至少为a[j].val;
            if(j==0){    //最后一步为++j,此时的j为(j_pre+1)
                tmin=A;
            }
            else{
                tmin=min((left+s[j])/j,(LL)A); //最小值<=A
            }
            tmp+=(LL)tmin*cm;
            if(ans<tmp){
                ans=tmp;
                ansi=i;      //[i,n-1]为perfect
                ansj=j-1;    //[0,j-1]为最小值tmin;
                ansmin=tmin;
            }
        }
        for(i=ansi;i<n;++i){
            a[i].val=A;
        }
        for(i=0;i<=ansj;++i){
            a[i].val=ansmin;
        }
        sort(a,a+n,cmp2);
        cout<<ans<<endl;
        for(i=0;i<n;++i){
            printf("%d ",a[i].val);
        }
        puts("");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u011721440/article/details/50527605