「日常训练」Skills(Codeforce Round #339 Div.2 D)
生活随笔
收集整理的這篇文章主要介紹了
「日常训练」Skills(Codeforce Round #339 Div.2 D)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題意(CodeForces 614D)
每個人有\(n(n\le 10^5)\)個技能,技能等級都在\([0,10^9]\)的范圍,每個技能有一個當前等級,所有技能的最高等級都為A。一個人的力量被記做以下兩項的和:
1. 頂級技能的個數(shù) *cf
2. 最低等級的技能 *cm
每個單位的錢能夠提升一級力量。我們希望花盡可能少的錢,使得力量盡可能高。
分析
我二分的功力還是不足,要多努力。這題其實是一個非常明顯的暴力:我們枚舉提高到A的等級的個數(shù)(到不能提升為止),枚舉這種情況下,我們能夠令把多少人的等級提升到相同的某個等級——這個地方可以用二分解決(先排序)。這樣遍歷一遍整個等級,就能得到最優(yōu)的策略,然后輸出答案即可。具體的代碼細節(jié)見注釋。
代碼
/** Filename: cfr339d2d.cpp* Date: 2018-11-07*/#include <bits/stdc++.h>#define INF 0x3f3f3f3f #define PB emplace_back #define MP make_pair #define fi first #define se second #define rep(i,a,b) for(repType i=(a); i<=(b); ++i) #define per(i,a,b) for(repType i=(a); i>=(b); --i) #define ZERO(x) memset(x, 0, sizeof(x)) #define MS(x,y) memset(x, y, sizeof(x)) #define ALL(x) (x).begin(), (x).end()#define QUICKIO \ios::sync_with_stdio(false); \cin.tie(0); \cout.tie(0); #define DEBUG(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)using namespace std; using pi=pair<int,int>; using repType=int; using ll=long long; using ld=long double; using ull=unsigned long long;const int MAXN=100005;struct Skill {int v,o;Skill() {}Skill(int _v, int _o):v(_v), o(_o) {} } a[MAXN];int n; ll A,cf,cm,m,sum[MAXN];int solve(int R, ll now) // given present cost, and r which tells 1~r are not A, { // which lowest lv can you reach?if(R==0) return A;int l=1, r=R;while(l<r){int mid=(l+r+1)/2;ll need=ll(a[mid].v)*mid-sum[mid]; // we need it to make pre n peopleif (need>now) r=mid-1; else l=mid; // reach the lv of the nth.}ll need=ll(a[l].v)*l-sum[l];ll more=(now-need)/l;return min(ll(A), a[l].v+more); }int main() {scanf("%d%lld%lld%lld%lld", &n, &A, &cf, &cm, &m);rep(i,1,n){scanf("%d", &a[i].v);a[i].o=i;}sort(a+1, a+n+1, [](Skill& x, Skill& y) -> bool{return x.v<y.v;});a[n+1].v=A;rep(i,1,n) sum[i]=sum[i-1]+a[i].v;ll ans=-1, cost=0;int v,p;per(i,n,0){cost+=A-a[i+1].v; if(cost>m) break;int minv=solve(i,m-cost);ll tmp=minv*cm+(n-i)*cf;if(tmp>ans){ans=tmp;p=i;v=minv;}}printf("%lld\n", ans);per(i,n,p+1) a[i].v=A;rep(i,1,p) a[i].v=max(a[i].v, v);sort(a+1, a+n+1, [](Skill& x, Skill& y) -> bool{return x.o<y.o;});rep(i,1,n)printf("%d ", a[i].v);printf("\n");return 0; }轉載于:https://www.cnblogs.com/samhx/p/CFR339D2D.html
總結
以上是生活随笔為你收集整理的「日常训练」Skills(Codeforce Round #339 Div.2 D)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。