[CodeForces - 614D] D - Skills

D - Skills

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?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 ai (ai?≤?ai?≤?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.

Example

Input

3 5 10 1 51 3 1

Output

122 5 2 

Input

3 5 10 1 3391 3 1

Output

355 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.

题目的意思是,有几个技能,每个技能有一个初始等级,所有技能有一个共同的最大等级限度.现在你有几次升级的机会,要求升级之后,某个值最大化.

其中,这个值的计算方式是,满级的个数*常数1+等级最低值*常数2.

那么,我们可以枚举满级的个数,然后二分求解最小值,并且尽可能使其最大化.

假设我们现在枚举了有i个满级的,那么,要计算出剩下的钱还有多少,然后进行二分答案,枚举low,表示想让所有的等级都不小于low.

那么原来等级小于low的肯定要升级,花钱.如果钱够花就可以.

但是又有一个问题,在枚举+二分时,已经是nlogn的复杂度了,再套个n肯定TLE.而我们有需要知道,到底有几个等级小于low,以及总共少了多少.那么,对于这个问题,我们可以通过二分查找来实现.

所以,最后的复杂度是nlogn^2(注意,在更新答案的时候不要o(n)更新!!!可以取几个关键的值,最后再更新,还要注意数据类型).

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=100005;
 6 int n,A,out[maxn];
 7 struct data{
 8     int x,i;
 9     bool operator < (const data &u) const {return x<u.x;}
10 }cur[maxn];
11 long long cf,cm,m;
12 long long s[maxn],ans;
13 int f1,f2,f3;
14 inline long long read(){
15     long long x=0; char ch=getchar();
16     while (ch<‘0‘||ch>‘9‘) ch=getchar();
17     while (ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();
18     return x;
19 }
20 int bis(int low){
21     int l=1,r=n,md;
22     if (low<=cur[l].x) return 0;
23     if (low>cur[r].x) return r;
24     while (l<=r){
25         md=(l+r)>>1;
26         if (cur[md-1].x<low&&low<=cur[md].x) return md-1;
27         if (low<=cur[md-1].x) r=md-1; else if (low>cur[md].x) l=md+1;
28     }
29 }
30 bool jug(int low,long long money,int num){
31     int x=min(n-num,bis(low));
32     long long s1=s[x],s2=(long long)low*x;
33     if (s1+money>=s2){
34         if (ans>=cf*num+cm*low) return 1;
35         ans=cf*num+cm*low; f1=low; f2=x; f3=num;
36         return 1;
37     }
38     return 0;
39 }
40 int main(){
41     n=(int)read(),A=(int)read(),cf=read(),cm=read(),m=read(),s[0]=0;
42     for (int i=1; i<=n; i++) cur[i].x=(int)read(),cur[i].i=i; cur[n+1].x=A;
43     sort(cur+1,cur+1+n);
44     memset(out,0,sizeof out);
45     ans=cm*cur[1].x;
46     for (int i=1; i<=n; i++) if (cur[i].x==A) ans+=cf;
47     for (int i=1; i<=n; i++) out[cur[i].i]=cur[i].x;
48     for (int i=1; i<=n; i++) s[i]=s[i-1]+cur[i].x;
49     long long sum=0;
50     for (int i=0; i<=n; i++){
51         if (A>cur[n-i+1].x) sum+=A-cur[n-i+1].x;
52         if (sum>m) break;
53         int L=cur[1].x,R=A,mid;
54         while (L<=R){
55             mid=(L+R)>>1;
56             if (jug(mid,m-sum,i)) L=mid+1; else R=mid-1;
57         }
58     }
59     memset(out,0,sizeof out);
60     for (int i=1; i<=f2; i++) out[cur[i].i]=f1;
61     for (int i=f2+1; i<=n-f3; i++) out[cur[i].i]=cur[i].x;
62     for (int i=n-f3+1; i<=n; i++) out[cur[i].i]=A;
63     printf("%lld\n",ans);
64     for (int i=1; i<=n; i++) printf("%d ",out[i]);
65     return 0;
66 }

时间: 2025-01-15 03:52:49

[CodeForces - 614D] D - Skills的相关文章

CodeForces 614D Skills

排序+枚举+二分 最大的那些变成A,小的那部分提高最小值 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn=100000+10; int n; long long A,cf,cm,m; struct X { long long a; long long ans; int id; } s[maxn]

Codeforces 614D 二分

D. Skills 题意: 给出 n, A, cf, cm, m,表示有 n 个技能,每个技能当前水平为 a[i] ,最高水平为 A ,有 m 个技能点,花费一个技能点可以使任意一个技能水平加一 (最高只能是 A). 如果最后水平为 A 的技能有 x 个,最低的技能水平值为 y,定义权值为 cf*x+cm*y .求可能的最高权值,并输出最后每个技能的水平. tags: 枚举 x ,对当前 x 二分可能的最低水平值,check 时还要再二分一下. // 614D #include<bits/std

Codeforces 581C Developing Skills

C. Developing Skills Petya loves computer games. Finally a game that he's been waiting for so long came out! The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number 

「日常训练」Skills(Codeforce Round Div.2 #339 D)

题意(CodeForces 614D) 每个人有\(n(n\le 10^5)\)个技能,技能等级都在\([0,10^9]\)的范围,每个技能有一个当前等级,所有技能的最高等级都为A.一个人的力量被记做以下两项的和: 1. 顶级技能的个数 *cf 2. 最低等级的技能 *cm 每个单位的钱能够提升一级力量.我们希望花尽可能少的钱,使得力量尽可能高. 分析 我二分的功力还是不足,要多努力.这题其实是一个非常明显的暴力:我们枚举提高到A的等级的个数(到不能提升为止),枚举这种情况下,我们能够令把多少人

CodeForces 42A Guilty — to the kitchen!

Guilty — to the kitchen! Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 42A Description It's a very unfortunate day for Volodya today. He got bad mark in algebra and was therefore force

Codeforces Round #277.5 (Div. 2) JAVA版题解

Codeforces Round #277.5 (Div. 2) A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output In this problem your goal is to sort an array consisting of n integers in at most n swaps. For t

codeforces 489B. BerSU Ball 解题报告

题目链接:http://codeforces.com/problemset/problem/489/B 题目意思:给出 n 个 boys 的 skills 和 m 个 girls 的 skills,要求成 pair 的条件为,男和女的 skill 差值最多只能为 1.问成 pair 最多是多少. 这个不太难,关键是要考虑周全. 对于某个人,假设他(男)的 skill 值为 i,那么跟他成 pair 的人的 skill 值,只有三种:i-1,i,i+1.跟他成对的女要减少一个.然后女的那边也要搜一

Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】

传送门:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A multi-subject competition is coming! The competition has mm 

Codeforces Round #587 (Div. 3) B - Shooting

原文链接:https://www.cnblogs.com/xwl3109377858/p/11564214.html Codeforces Round #587 (Div. 3) B - Shooting Recently Vasya decided to improve his pistol shooting skills. Today his coach offered him the following exercise. He placed n cans in a row on a ta