Codeforces Round #643 (Div. 2) E. Restorer Distance (贪心,三分)
2024-08-31 08:39:52
题意:给你\(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;
}
最新文章
- Selector
- Unreal模型文件调查
- css布局之两列布局
- The main concepts
- 【wikioi】1010 过河卒
- ASCII码对照表 (转)
- Calculations are rather interesting
- C 基于socket实现简单的文件传输
- JS实现登录页面记住密码和enter键登录
- ngnix 配置
- 【大盛】HTC one/M7 ROM 最新本地化OrDroid8.2.6 高级、快速设置 永久root 更多自定义 稳定 流畅
- MTK Android 默认值修改笔记
- 依赖注入及AOP简述(九)——单例和无状态Scope .
- keepalived原理
- .net string类型集合转int集合
- 使用min-device-pixel-ratio媒体功能实现真正的1像素border
- G1垃圾收集器的实现原理
- python面对对象编程中会用到的装饰器
- Codeforces 835F Roads in the Kingdom - 动态规划
- Android笔记:OptionsMenu