• 题意:给你\(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;
    }

Codeforces Round #643 (Div. 2) E. Restorer Distance (贪心,三分)的更多相关文章

  1. Codeforces Round #643 (Div. 2)(C ~ E)

    C. Count Triangles 题目链接 : https://codeforces.com/contest/1355/problem/C 题目大意 : 给你 A , B , C , D 问有多少 ...

  2. Codeforces Round #643 (Div. 2) 题解 (ABCDE)

    目录 A. Sequence with Digits B. Young Explorers C. Count Triangles D. Game With Array E. Restorer Dist ...

  3. Codeforces Round #643 (Div.2)

    前言:这套cf我感觉出的很不错,AB就不说了,唯一有点欠缺的就是C和D的位置应该换一下,C出的挺不错,反正我当时没有想出来(赛后补题的时候其实也不难..听朋友说还可以FFT优化,然而我是个图论手并不会 ...

  4. Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和

    B. Hamming Distance Sum 题目连接: http://www.codeforces.com/contest/608/problem/A Description Genos need ...

  5. Codeforces Round #336 (Div. 2) B. Hamming Distance Sum 计算答案贡献+前缀和

    B. Hamming Distance Sum   Genos needs your help. He was asked to solve the following programming pro ...

  6. Codeforces Round #650 (Div. 3) C. Social Distance

    题目链接:https://codeforces.com/contest/1367/problem/C 题意 给出一个长为 $n$ 的 $01$字符串,两个相邻 $1$ 间距应大于 $k$,初始序列合法 ...

  7. Codeforces Round #643 (Div. 2) B. Young Explorers (思维,贪心)

    题意:给你一组人\(a\),现在要将这些人进行分组,对于\(i\),只有某一组的人数\(\ge a_{i}\)时,\(i\)才可以加入这个组,问最多能够有多少组,(不必将所有人都选用). 题解:我们将 ...

  8. Codeforces Round #643 (Div. 2) C. Count Triangles (数学公式)

    题意:给你四个正整数\(A,B,C,D\),且\(A\le B\le C \le D\),有\(A\le x\le B\le y\le C \le z\le D\),求最多有多少组\((x,y,z)\ ...

  9. Codeforces Round #643 (Div. 2) D. Game With Array (思维,构造)

    题意:给你两个正整数\(N\)和\(S\),构造一个长度为\(N\)并且所有元素和为\(S\)的正整数数组,问是否能找到一个\(K (0\le K \le S)\)使得这个数组的任意_子数组_的和都不 ...

随机推荐

  1. (十八)configparser模块

    configparser模块一般是用来处理配置文件的,如: [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel ...

  2. 【Mysql】[Err] 1153 - Got a packet bigger than 'max_allowed_packet' bytes

    今天用Navicat导入的时候报错 [Err] 1153 - Got a packet bigger than 'max_allowed_packet' bytes 原因是数据库默认是16M的数据,这 ...

  3. SQL语句中 ` 的作用

    SQL语句中 ` 的作用 做攻防世界WEB区 supersqli 题目,在构建SQL语句时,遇到SQL语句中有 ` 时可以解析,没有则不能. 查阅资料得知,` 通常用来说明其中的内容是数据库名.表名. ...

  4. (四)React Ant Design Pro + .Net5 WebApi:PostgreSQL数据库环境搭建

    一.简介 PostgreSQL,开源数据库(没听过小伙伴自己反思一下自行百度) PgAdmin,官方提供的数据库管理工具. 二.环境 1. 官网下载包,安装数据库 tar xjvf /app/pack ...

  5. 动态sql语句、逆向工程(generator)、分页助手(pagehelper)

    1.动态sql语句 if if where 配合使用 <select id="selectByWhere" resultType="com.alibaba.wlq. ...

  6. HTML基础复习3

    CSS 可以理解为对HTML的一种补充 CSS由两部分组成:选择器.声明,声明中包含属性和值 CSS中的选择器 HTML标签选择器 类选择器 在标签上使用class属性为标签起个类名,在CSS中使用. ...

  7. .net code+vue 文件上传

    后端技术 .net code 官方文档 https://docs.microsoft.com/zh-cn/aspnet/core/mvc/models/file-uploads?view=aspnet ...

  8. css animation @keyframes 动画

    需求:语音播放动态效果 方案:使用如下图片,利用 css animation @keyframes  做动画 html <span class="horn" :class=& ...

  9. 浅谈linux IO csy 360技术 2021-01-18

    浅谈linux IO csy 360技术 2021-01-18

  10. 高性能数据导入方案&表过滤器&一对多支持筛选- .NET SqlSugar ORM

    一.数据导入有哪些难题 1.数据分类 你需要将 插入.更新.忽略不计.错误数据 等进么分类汇总,最后返回给客户,如果没有很好的设计想把这些操作一步到位非常的难 2.高性能 对于插入或者更新 肯定不能单 ...