【CF1355E】Restorer Distance(整数三分)--附整数/浮点三分模板

传送门

  • 题目:
    【CF1355E】Restorer Distance(整数三分)--附整数/浮点三分模板_第1张图片
  • 解题思路:
    存在这么一个高度h使得代价最小,其他高度的代价都比它大。这是一个凹函数,可以三分来求解。
    附三分模板:转载自https://www.cnblogs.com/lukelmouse/p/12545973.html
//整数三分
int l = 1,r = 100;
while(l < r) {
    int lmid = l + (r - l) / 3;
    int rmid = r - (r - l) / 3;
    lans = f(lmid),rans = f(rmid);
    // 求凹函数的极小值
    if(lans <= rans) r = rmid - 1;
    else l = lmid + 1;
    // 求凸函数的极大值
    if(lasn >= rans) l = lmid + 1;
    else r = rmid - 1;
}
// 求凹函数的极小值
cout << min(lans,rans) << endl;
// 求凸函数的极大值
cout << max(lans,rans) << endl;
//浮点三分
const double EPS = 1e-9;
while(r - l < EPS) {
    double lmid = l + (r - l) / 3;
    double rmid = r - (r - l) / 3;
    lans = f(lmid),rans = f(rmid);
    // 求凹函数的极小值
    if(lans <= rans) r = rmid;
    else l = lmid;
    // 求凸函数的极大值
    if(lans >= rans) l = lmid;
    else r = rmid;
}
// 输出 l 或 r 都可
cout << l << endl;
  • ac代码:
#include 
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
int N, A, R, M;
int H[maxn];
ll count(int h)
{
    ll sub = 0, add = 0, res = 0;
    for(int i = 1; i <= N; i++)
        if(H[i]<h) add+=h-H[i];
        else sub+=h-H[i];
    if(M<A+R)
    {
        ll mi = min(abs(sub), add), dif = sub+add;
        res+=mi*M;
        if(abs(sub)<add) res+=abs(dif)*A;
        else res+=abs(dif)*R;
    }
    else res = abs(sub)*R+add*A;
    return res;
}

int main()
{
    //freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
    scanf("%d%d%d%d", &N, &A, &R, &M);
    int l = 0, r = 0;
    for(int i = 1; i <= N; i++) scanf("%d", &H[i]), r = max(H[i], r);
    while(l<r)
    {
        int midl = l+(r-l)/3;
        int midr = r-(r-l)/3;
        if(count(midl)>=count(midr)) l = midl+1;
        else r = midr-1;
    }
    printf("%lld\n", count(l));
    return 0;
}

你可能感兴趣的:(二分/三分,codeforces,算法,三分)