• 题意:给你\(n\)个数,每次可以使某个数++,--,或使某个数--另一个++,分别消耗\(a,r,m\).求使所有数相同最少的消耗.

  • 题解:因为答案不是单调的,所以不能二分,但不难发现,答案只有一个峰,所以我们可以三分高度,然后写个check函数贪心一下即可.

  • 代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <map>
    #include <set>
    #include <unordered_set>
    #include <unordered_map>
    #define ll long long
    #define fi first
    #define se second
    #define pb push_back
    #define me memset
    const int N = 1e6 + 10;
    const int mod = 1e9 + 7;
    using namespace std;
    typedef pair<int,int> PII;
    typedef pair<long,long> PLL; int n,a,r,m;
    ll h[N]; ll check(ll x){
    ll sum=0,sum1=0,sum2=0;
    if(a+r<=m){
    for(int i=1;i<=n;++i){
    if(h[i]>x) sum+=(h[i]-x)*r;
    else if(h[i]<x) sum+=(x-h[i])*a;
    }
    }
    else{
    for(int i=1;i<=n;++i){
    if(h[i]>x) sum1+=(h[i]-x);
    else if(h[i]<x) sum2+=(x-h[i]);
    }
    ll t=min(sum1,sum2);
    sum+=t*m;
    sum+=(sum1-t)*r+(sum2-t)*a;
    }
    return sum;
    } int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>n>>a>>r>>m;
    for(int i=1;i<=n;++i) cin>>h[i]; ll l=0,ri=1e9;
    while(l+20<ri){
    ll mid1=l+(ri-l)/3;
    ll mid2=ri-(ri-l)/3;
    if(check(mid1)>check(mid2)) l=mid1;
    else ri=mid2;
    }
    ll res=2e18;
    for(ll i=l;i<=ri;++i) res=min(res,(ll)check(i));
    printf("%lld\n",res); return 0;
    }
  • 三分模板:

    因为是第一次写三分,所以这里贴一个三分模板.还是非常简单的.

        int l=0,r=1e9;
    while(l+2<r){
    int midl=l+(r-l)/3; //左边1/3
    int midr=r-(r-l)/3; //右边1/3
    if(check(midl)>check(midr)) l=midl;
    else r=midr;
    }

最新文章

  1. Selector
  2. Unreal模型文件调查
  3. css布局之两列布局
  4. The main concepts
  5. 【wikioi】1010 过河卒
  6. ASCII码对照表 (转)
  7. Calculations are rather interesting
  8. C 基于socket实现简单的文件传输
  9. JS实现登录页面记住密码和enter键登录
  10. ngnix 配置
  11. 【大盛】HTC one/M7 ROM 最新本地化OrDroid8.2.6 高级、快速设置 永久root 更多自定义 稳定 流畅
  12. MTK Android 默认值修改笔记
  13. 依赖注入及AOP简述(九)——单例和无状态Scope .
  14. keepalived原理
  15. .net string类型集合转int集合
  16. 使用min-device-pixel-ratio媒体功能实现真正的1像素border
  17. G1垃圾收集器的实现原理
  18. python面对对象编程中会用到的装饰器
  19. Codeforces 835F Roads in the Kingdom - 动态规划
  20. Android笔记:OptionsMenu

热门文章

  1. ThinkPHP5表单令牌刷新
  2. Linux学习笔记 | 将默认镜像源修改为国内镜像源
  3. (十七)logging模块
  4. 【Linux】Linux进程间通信的几种方式
  5. ctfhub技能树—web前置技能—http协议—302跳转
  6. 24v转3.3v稳压芯片,高效率DC-DC变换器3A输出电流
  7. Redis 实战 —— 05. Redis 其他命令简介
  8. 使用npm install安装项目依赖的时候报错
  9. uni-app开发经验分享十四:小程序超过2M限制的方法——分包加载
  10. Typora+PicGo+Gitee打造图床