codeforces#439 D. Devu and his Brother (二分)
题意:给出a数组和b数组,他们的长度最大1e5,元素范围是1到1e9,问你让a数组最小的数比b数组最大的数要大需要的最少改变次数是多少。每次改变可以让一个数加一或减一
分析:枚举a数组和b数组的所有的元素x,作为他们的界限,也就是说a数组所有的数要大于等于x,b数组所有的数要小于等于x,再利用前缀和+二分,分别求出ab数组需要改变的次数,在所有的方案中取一个最小值
代码:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10;
ll sum1[maxn],sum2[maxn];
int num1[maxn],num2[maxn];
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&num1[i]);
for(int i=1;i<=m;i++)
scanf("%d",&num2[i]);
sort(num1+1,num1+1+n);
sort(num2+1,num2+1+m);
for(int i=1;i<=n;i++)
sum1[i]=sum1[i-1]+num1[i];
for(int i=1;i<=m;i++)
sum2[i]=sum2[i-1]+num2[i];
ll ans=1e18;
for(int a=1;a<=n+m;a++)
{
int i;
if(a<=n)i=num1[a];
else i=num2[a-n];
ll ans1,ans2;
if(num1[1]>=i)ans1=0;
else
{
int st=1,en=n;
while(st!=en)
{
int md=(st+en)/2;
if(num1[md+1]<=i)st=md+1;//可以修改的下标
else en=md;
}
ans1=(ll)i*st-sum1[st];
}
if(num2[m]<=i)ans2=0;
else
{
int st=1,en=m;
while(st!=en)
{
int md=(st+en)/2;
if(num2[md]>=i)en=md;
else st=md+1;
}
ans2=sum2[m]-sum2[st-1]-(ll)i*(m-st+1);
}
//cout<<i<<" "<<ans1<<" "<<ans2<<endl;
ans=min(ans1+ans2,ans);
}
printf("%lld\n",ans);
return 0;
}
codeforces#439 D. Devu and his Brother (二分)的更多相关文章
- Codeforces 439 A. Devu, the Singer and Churu, the Joker
这是本人第一次写代码,难免有点瑕疵还请见谅 A. Devu, the Singer and Churu, the Joker time limit per test 1 second memory l ...
- codeforces 439 E. Devu and Birthday Celebration 组合数学 容斥定理
题意: q个询问,每一个询问给出2个数sum,n 1 <= q <= 10^5, 1 <= n <= sum <= 10^5 对于每一个询问,求满足下列条件的数组的方案数 ...
- codeforces 251 div2 D. Devu and his Brother 三分
D. Devu and his Brother time limit per test 1 second memory limit per test 256 megabytes input stand ...
- CF 439D(251D题)Devu and his Brother
Devu and his Brother time limit per test 1 second memory limit per test 256 megabytes input standard ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- Codeforces Round#251(Div 2)D Devu and his Brother
--你以为你以为的.就是你以为的? --有时候还真是 题目链接:http://codeforces.com/contest/439/problem/D 题意大概就是要求第一个数组的最小值要不小于第二个 ...
- Codeforces 439D Devu and his Brother 三分
题目链接:点击打开链接 = - =曾经的三分姿势不对竟然没有被卡掉,,,太逗.. #include<iostream> #include<string> #include< ...
- Codeforces 749D:Leaving Auction(set+二分)
http://codeforces.com/contest/749/problem/D 题意:有几个人在拍卖场竞价,一共有n次喊价,有q个询问,每一个询问有一个num,接下来num个人从这次拍卖中除去 ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
随机推荐
- Spring Cloud Alibaba与Spring Boot、Spring Cloud之间不得不说的版本关系
这篇博文是临时增加出来的内容,主要是由于最近连载<Spring Cloud Alibaba基础教程>系列的时候,碰到读者咨询的大量问题中存在一个比较普遍的问题:版本的选择.其实这类问题,在 ...
- C# 如何隐藏或显示工作表中的网格线
我们知道Excel中有许多虚线形式的网格线,它们用于区分Excel工作表中的单元格.有了网格线,读者可以轻松地查看和核对工作表中的数据.Excel工作表中,网格线是默认存在的,但我们可以根据自身的需求 ...
- centos-linux入门笔记
一.linux的用户 linux的用户和windwos差不多,分为管理员用户和普通用户. 普通用户:权限没那么高而已. 管理员用户:在linux中就是root用户,所拥有的权限是最高的. 二.linu ...
- c++中求数组长度
#include <iostream> using namespace std; template <class T> int getArrSize(T& arr){ ...
- Ajax请求中的async:false/true(同步/异步)的作用
async. 默认是true,即为异步方式,$.Ajax执行后,会继续执行ajax后面的脚本,直到服务器端返回数据后,触发$.Ajax里的success方法,这时候执行的是两个线程.若要将其设置为fa ...
- vue结合axios使用入门
Vue官方推荐的网络通信库不再是vue-resource了,推荐使用axios axios安装 npm: $ npm install axios bower: $ bower install axio ...
- cesium 之自定义气泡窗口 infoWindow 后续优化篇(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 该 ...
- arcgis api 3.x for js 入门开发系列七图层控制(附源码下载)
前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...
- Android开发,关于如何在应用间共享SharedPreference
开发一个应用,需要用到两个应用A和B之间共享数据的问题,这个数据是个单一的数据,所以就想用SharedPrefernce来进行保存. 使用网上的各种应用间的共享代码,B是读取A的数据,所以代码为: C ...
- 2019Java查漏补缺(三)
1.为什么这个public的类的类名必须和文件名相同 是为了方便虚拟机在相应的路径中找到相应的类所对应的字节码文件 2.java8 的一些新特性: 3: 数据库隔离级别 隔离级别 ...