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 ...
随机推荐
- php 的优化
=>PHP函数禁用 disable_functions = phpinfo,passthru,exec,system,popen,chroot,escapeshellcmd,escapeshel ...
- LeetCode 178. 分数排名
1.题目描述 编写一个 SQL 查询来实现分数排名.如果两个分数相同,则两个分数排名(Rank)相同.请注意,平分后的下一个名次应该是下一个连续的整数值.换句话说,名次之间不应该有“间隔”. +--- ...
- WEB前端常见面试题汇总:(一)
1.JS找字符串中出现最多的字符 例如:求字符串'nininihaoa'中出现次数最多字符 方法一: var str = "nininihaoa"; var o = {}; for ...
- 【20190405】JavaScript-整理一些常用正则式
匹配中文字符: let reg=/([\u4E00-\u9FFF]+)/; //\u代表Unicode编码 匹配电话号码: let reg=/^1[34578]\d{9}$/; 给每三位数字添加一个逗 ...
- #WEB安全基础 : HTML/CSS | 0x11 浅谈GET和POST
HTTP中的GET和POST请求方法 我上次提到了GET和POST,现在就让你来认识一下这些新朋友 请看图 POST和GET都是将用户输入到浏览器的数据发送给服务器,不过采用了两种不同的方式,POST ...
- Neutron server的运行原理(未完待续)
1.Neutron server首先是一个web server, 对于http和https协议的报文进行响应. 2.Neutron server进程里面包含了一个WSGI 应用程序,以及不同模块的pl ...
- arcgis for js学习之Graphic类
arcgis for js学习之Graphic类 <title>Graphic类</title> <meta charset="utf-8" /> ...
- iOS----------随机色
#define KColorRandomColor [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 ...
- idea部署Maven入门(一)——环境变量的配置和下载
介绍: 1 Maven是用来管理jar包的一种工具, 2 Maven主要是构建java项目和java web项目 3 maven项目管理所依赖的jar ...
- MySQL 处理海量数据时的一些优化查询速度方法
查询速度慢的原因 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O 吞吐量小,形成了瓶颈效应. 3.没有创建计算列导致查询不优化. 4.内存不足 5.网络速度慢 6 ...