613B Skills(列舉+二分)
B. 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
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
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.
Examples
input
Copy
3 5 10 1 5 1 3 1
output
Copy
12 2 5 2
input
Copy
3 5 10 1 339 1 3 1
output
Copy
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.
解題思路:先對原序列排序,然後從後往前列舉填滿的個數。然後對於前面的進行二分,看最大能把最小值填到多大。然後更新答案即可。這裡很多細節要注意!
#include <bits/stdc++.h>
using namespace std;
const int MAXN=100005;
typedef long long ll;
pair<ll,int> a[MAXN];
bool cmp(pair<ll,int> &a,pair<ll,int> &b){
return a.second<b.second;
}
ll need[MAXN];//把前i-1個填到跟第i個相同,需要多少次
ll sum[MAXN];//把前i個填到A,需要多少次
int main(){
int N;
ll A,CF,CM,M;
scanf("%d%lld%lld%lld%lld",&N,&A,&CF,&CM,&M);
for(int i=1;i<=N;i++){
scanf("%lld",&a[i].first);
a[i].second=i;
}
sort(a+1,a+1+N);
int num=0;
int yuan=N;
while(a[N].first==A)
{
num++;
N--;
}
for(int i=1;i<=N;i++){
need[i]=need[i-1]+(a[i].first-a[i-1].first)*(i-1);
sum[i]=sum[i-1]+a[i].first;
}
ll ans=0;
int ansindex=0;
for(int i=0;i<=N;i++)//列舉填滿的個數
{
ll tmp=M;
ll res=CF*(num+i);
tmp-=i*A-(sum[N]-sum[N-i]);//直接計算要多少次
if(tmp<0)
break;
if(i==N)//特殊處理
{
res+=CM*A;
if(res>ans){
ans=res;
ansindex=i;
}
break;
}
int l=1,r=N-i+1;//邊界要注意細節
while(l<r){
int m=(l+r)/2;
if(need[m]<=tmp)
l=m+1;
else
r=m;
}
int index=l-1;
if(index==0)
index=1;
ll minn=a[index].first;
tmp-=need[index];
minn+=tmp/index;//計算最小能去到多大,
if(minn>A)
minn=A;
res+=minn*CM;
if(res>ans){
ans=res;
ansindex=i;
}
}
//把答案計算出來
ll tmp=M;
for(int j=N;j>=N-ansindex+1;j--){
tmp-=A-a[j].first;
a[j].first=A;
}
int l=1,r=N-ansindex+1;
while(l<r){
int m=(l+r)/2;
if(need[m]<=tmp)
l=m+1;
else
r=m;
}
int index=l-1;
if(index==0)
index=1;
tmp-=need[index];
ll num1=tmp/index;
for(int i=1;i<=index;i++){
a[i].first=a[index].first+num1;
a[i].first=min(A,a[i].first);
}
ll mod=tmp%index;
for(int i=1;i<=mod;i++){
a[i].first++;
a[i].first=min(A,a[i].first);
}
sort(a+1,a+yuan+1,cmp);
cout<<ans<<endl;
for(int i=1;i<=yuan;i++)
cout<<a[i].first<<" ";
cout<<endl;
return 0;
}