——你以为你以为的。就是你以为的?

——有时候还真是

题目链接:http://codeforces.com/contest/439/problem/D

题意大概就是要求第一个数组的最小值要不小于第二个数组的最大值,你所能做的就是对数组中的某一个数进行+1/-1的操作。最后问操作次数最少须要多少次。

起初这题我看着例子就開始瞎YY了,YY思路如图= =|||

看似有那么一丁点道理,可是事实上是无法这么找到的,非常快就自己找到了反例。。

只是之所以说是有那么一点道理是由于例子中的边缘数正好是它的平衡点,所谓平衡点就是将第一个数组里的数提升到某一个数a之上,把第二个数组的全部数降到数a之下。这就好比将一个天平平衡了。也就是最优解了。

可随之而来的问题也就非常明了了。怎样找到哪个平衡数呢?

我们最好还是先把两个数组里的数存到同一个数组里。然后升序排序。这样我们就得到了一条线性的数列,那么既然是n+m个数里前m个要比后n个小,也就非常自然的能找到平衡点为数列其中的第m或者第m+1个数了。

#include<cstdio>
#include<algorithm>
#define MAX(a,b) ((a>b)?(a):(b))
using namespace std;
int a[1000010],b[1000010],c[2000020];
int main(){
int n,m;
long long ans= 0LL;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++) scanf("%d",&a[i]),c[i]=a[i];
for(int i=0;i<m;i++) scanf("%d",&b[i]),c[n+i]=b[i];
sort(c,c+m+n);
int temp=c[m];
for(int i=0;i<n;i++) ans+=MAX(0,(temp-a[i]));
for(int i=0;i<m;i++) ans+=MAX(0,(b[i]-temp));
printf("%I64d\n",ans);
return 0;
}

Codeforces Round#251(Div 2)D Devu and his Brother的更多相关文章

  1. Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Array

    注意p的边界情况,p为0,或者 p为k 奇数+偶数 = 奇数 奇数+奇数 = 偶数 #include <iostream> #include <vector> #include ...

  2. Codeforces Round #251 (Div. 2) B. Devu, the Dumb Guy

    注意数据范围即可 #include <iostream> #include <vector> #include <algorithm> using namespac ...

  3. Codeforces Round #251 (Div. 2) A - Devu, the Singer and Churu, the Joker

    水题 #include <iostream> #include <vector> #include <algorithm> using namespace std; ...

  4. Codeforces Round 251 (Div. 2)

    layout: post title: Codeforces Round 251 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  5. Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥

    E. Devu and Flowers 题目连接: http://codeforces.com/contest/451/problem/E Description Devu wants to deco ...

  6. Codeforces Round #258 (Div. 2)E - Devu and Flowers

    题意:n<20个箱子,每个里面有fi朵颜色相同的花,不同箱子里的花颜色不同,要求取出s朵花,问方案数 题解:假设不考虑箱子的数量限制,隔板法可得方案数是c(s+n-1,n-1),当某个箱子里的数 ...

  7. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  8. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  9. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

随机推荐

  1. mysql之mysql、mysql-devel、mysql-server

    一.引言 一直都没搞明白这三者之间的关系,于是决心查资料把这个问题搞明白,遂记录以便查阅.学习 二.关系 mysql-devel 开发用到的库以及包含文件mysql mysql 客户端mysql-se ...

  2. eclipse导入svn中的maven工程项目

    Eclipse导入现有的maven工程 第一步:右键如图 new->other 进入other->输入svn->从svn检索项目,如图 点击next>如下图,如是第一次则选择创 ...

  3. Docker是用来干什么的?【快速入门】

    Docker从去年开始不仅能在Linux下运行 ,还支持windows.osX等主流系统. 下面的例子我自己经常使用,当然你有更好的案例也可以分享给我. 尝试新软件 对开发者而言,每天会催生出的各式各 ...

  4. EasyUI获取DataGrid中某一列的所有值

    function count() { var rows = $('#dg'').datagrid('getRows')//获取当前页的数据行 var total = 0; for (var i = 0 ...

  5. MySQL Migration Toolkit启动报jre错误

    正常启动的话,会报出“an error occurred during the initialization of the runtime system.Please make sure you ha ...

  6. 列出自己常用的jdk中的数据结构

    列出自己常用的jdk中的数据结构 解答:线性表,链表,哈希表是常用的数据结构.

  7. git上解决代码冲突(merge版)

    1.切换到master: git checkout master 2.拉最新代码:git pull origin master 3.切换到提交的分支:git checkout Txxxx 4.合并:g ...

  8. ios开发之 -- 调用系统定位获取当前经纬度与地理信息

    ios 10定位: 在info.plist中加入: //允许在前台使用时获取GPS的描述 定位权限:Privacy - Location When In Use Usage Description / ...

  9. springside4

    https://github.com/springside/springside4/wiki/Design Design 1. Web MVC Framwork: SpringMVC3.0 Restf ...

  10. iOS 友盟统计怎么用

    本文转载至 http://blog.csdn.net/woaifen3344/article/details/41284395 友盟统计UMAnalyticsiOS友盟统计iOS UMAnalytic ...