Skills CodeForces - 614D (贪心)

时间:2024-12-09 15:06:32

链接

大意: $n$门课, 第$i$门分数$a_i$, 可以增加共$m$分, 求$cnt_{mx}*cf+mi*cm$的最大值

$cnt_{mx}$为满分的科目数, $mi$为最低分, $cf$, $cm$为给定系数

暴力枚举达到满分的科目数, 再二分答案求出最低分的最大值, 复杂度是$O(nlognlogmx)$

似乎可以双指针省去二分答案的log, 不过懒得写了..

#include <iostream>
#include <algorithm>
#include <cstdio>
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; const int N = 4e5+10, INF = 0x3f3f3f3f;
int n, mx, cf, cm;
pii a[N];
ll m, s1[N], s2[N];
int f[N]; int chk(int cnt, int x) {
int t = lower_bound(a+1,a+1+n,pii(x,0))-a-1;
ll c = (ll)t*x-s1[t];
if (c>m) return 0;
if (t+cnt<=n) return c+s2[n-cnt+1]<=m;
c += s2[t+1], cnt -= n-t;
c += (ll)(mx-x)*cnt;
return c<=m;
} int main() {
scanf("%d%d%d%d%lld", &n, &mx, &cf, &cm, &m);
REP(i,1,n) scanf("%d", &a[i].x),a[i].y=i;
sort(a+1,a+1+n);
REP(i,1,n) s1[i]=s1[i-1]+a[i].x;
PER(i,1,n) s2[i]=s2[i+1]+mx-a[i].x;
ll ans = 0;
int cnt=-1, mi;
REP(i,0,n) {
if (s2[n-i+1]>m) break;
int l=0, r=mx, t=-1;
while (l<=r) {
int mid=(l+r)/2;
if (chk(i,mid)) t=mid,l=mid+1;
else r=mid-1;
}
if ((ll)i*cf+(ll)t*cm<=ans) continue;
ans = (ll)i*cf+(ll)t*cm;
cnt = i, mi = t;
}
if (cnt>=0) {
REP(i,1,n) a[i].x=max(a[i].x,mi);
PER(i,1,n) if (cnt) --cnt,a[i].x=mx;
}
REP(i,1,n) f[a[i].y]=a[i].x;
printf("%lld\n", ans);
REP(i,1,n) printf("%d ",f[i]);
puts("");
}