Codeforecs 439D Devu and his Brother 三分
来源:互联网 发布:ubuntu安装32位运行库 编辑:程序博客网 时间:2024/05/16 19:36
点击打开链接
题意:给出两个序列a,b 操作:使某个元素增加1或者减少1
问使序列a中最小值>=序列b中最大值的最少操作次数? n,m<=1e5,a[i],b[i]<=1e9Proof: f(x)求导 f'(x)=cnt-num (cnt为a中小于x的个数,num为b中大于x的个数) x增大,f'(x)增大
f'(x)单调递增 f''(x)>=0 推出 f(x)为凸函数 三分求极值即可
#include <bits/stdc++.h>using namespace std;typedef long long ll;const double eps=1e-10;const int inf=0x3f3f3f3f;const int N=2e5+20;ll n,m,a[N],b[N];ll f(ll x){ll res=0;for(int i=1;i<=n;i++)res+=max(0ll,x-a[i]);for(int i=1;i<=m;i++)res+=max(0ll,b[i]-x);return res;}void Ternary_Search(){ll l=1,r=2e9,ans=2e16;while(l<r){ll m1=(l+r)/2,m2=(m1+r)/2;if(f(m1)>f(m2))l=m1;elser=m2;ans=min(ans,min(f(m1),f(m2)));}cout<<ans<<endl;}int main(){while(cin>>n>>m){for(int i=1;i<=n;i++)scanf("%I64d",&a[i]);for(int i=1;i<=m;i++)scanf("%I64d",&b[i]);Ternary_Search();}return 0;}
阅读全文
0 0
- Codeforecs 439D Devu and his Brother 三分
- Codeforces 439D Devu and his Brother 三分
- Codeforces 439D Devu and his Brother【思维+三分查找】
- 439 D. Devu and his Brother
- CF 439D Devu and his Brother
- Codeforces 439D Devu and his Brother
- Codeforces 439D Devu and his Brother
- CodeForces 439D Devu and his Brother
- Codeforces 439D Devu and his Brother(排序)
- Codeforces Round #251 (Div. 2) D. Devu and his Brother
- Codeforces Round#251(Div 2)D Devu and his Brother
- Codeforces Round #251 (Div. 2) D Devu and his Brother
- Codeforces Round #251 (Div. 2)-D. Devu and his Brother
- CF 439D(251D题)Devu and his Brother
- Codeforces Round #251 Div2D 439D Devu and his Brother 二分查找STL
- Codeforces Round #251 (Div. 2) 439D Devu and his Brother(脑洞)
- Codeforces Round #251(Div. 2) 439D. Devu and his Brother 枚举
- codeforecs--D. Tanya and Password(输出欧拉路径)
- Mac配置php.ini文件,连接mysql数据库
- C++中new和molloc的区别
- 【Java-1】写代码前的准备工作
- thinkPHP5隐藏入口文件index.php后 读取不到资源问题
- 机器学习基础:集成学习方法应用实验(RF、GBDT)
- Codeforecs 439D Devu and his Brother 三分
- jmap
- htm基础语句(补发)
- 博客开始第一天
- visual studio 2008中头文件和库文件路径设置
- 产生整形随机数 浮点型随机数
- SSM(一) | 框架整合
- Conflicting collector combinations using G1 GC
- Java时间和时间戳的相互转换