D. Devu and his Brother
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays a and bby their father. The array a is given to Devu and b to his brother.

As Devu is really a naughty kid, he wants the minimum value of his array a should be at least as much as the maximum value of his brother's array b.

Now you have to help Devu in achieving this condition. You can perform multiple operations on the arrays. In a single operation, you are allowed to decrease or increase any element of any of the arrays by 1. Note that you are allowed to apply the operation on any index of the array multiple times.

You need to find minimum number of operations required to satisfy Devu's condition so that the brothers can play peacefully without fighting.

Input

The first line contains two space-separated integers nm (1 ≤ n, m ≤ 105). The second line will contain n space-separated integers representing content of the array a (1 ≤ ai ≤ 109). The third line will contain m space-separated integers representing content of the array b (1 ≤ bi ≤ 109).

Output

You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition.

题意:两个序列,每次只能加一减一,使得第一个序列的最小值要大于等于第二个序列的最大值,求最少的操作次数;

思路:一个凹性函数,利用三分求答案;

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<string>
  5. #include<queue>
  6. #include<algorithm>
  7. #include<stack>
  8. #include<cstring>
  9. #include<vector>
  10. #include<list>
  11. #include<set>
  12. #include<map>
  13. using namespace std;
  14. #define ll long long
  15. #define mod 1000000007
  16. #define inf 999999999
  17. //#pragma comment(linker, "/STACK:102400000,102400000")
  18. int scan()
  19. {
  20. int res = , ch ;
  21. while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
  22. {
  23. if( ch == EOF ) return << ;
  24. }
  25. res = ch - '' ;
  26. while( ( ch = getchar() ) >= '' && ch <= '' )
  27. res = res * + ( ch - '' ) ;
  28. return res ;
  29. }
  30. ll a[];
  31. ll b[];
  32. ll check(ll x,ll y,ll cha)
  33. {
  34. ll sum=;
  35. for(ll i=;i<x;i++)
  36. if(cha>a[i])
  37. sum+=(cha-a[i]);
  38. for(ll i=;i<y;i++)
  39. if(cha<b[i])
  40. sum+=b[i]-cha;
  41. return sum;
  42. }
  43. int main()
  44. {
  45. ll x,y,z,i,t;
  46. scanf("%I64d%I64d",&x,&y);
  47. ll maxx=;
  48. ll minn=;
  49. for(i=;i<x;i++)
  50. {
  51. scanf("%I64d",&a[i]);
  52. minn=min(minn,a[i]);
  53. }
  54. for(i=;i<y;i++)
  55. {
  56. scanf("%I64d",&b[i]);
  57. maxx=max(maxx,b[i]);
  58. }
  59. if(minn>=maxx)
  60. {
  61. printf("0\n");
  62. return ;
  63. }
  64. ll st=minn;
  65. ll en=maxx;
  66. ll mid,midd;
  67. ll ans=min(check(x,y,minn),check(x,y,maxx));
  68. while(st<en)
  69. {
  70. mid=(st+en)/;
  71. midd=(mid+en)/;
  72. ll checkmid=check(x,y,mid);
  73. ll checkmidd=check(x,y,midd);
  74. if(checkmid>checkmidd)
  75. st=mid;
  76. else
  77. en=midd;
  78. ans=min(ans,min(checkmid,checkmidd));
  79. }
  80. printf("%I64d\n",ans);
  81. return ;
  82. }

codeforces 251 div2 D. Devu and his Brother 三分的更多相关文章

  1. codeforces 251 div2 C. Devu and Partitioning of the Array 模拟

    C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...

  2. Codeforces 439D Devu and his Brother 三分

    题目链接:点击打开链接 = - =曾经的三分姿势不对竟然没有被卡掉,,,太逗.. #include<iostream> #include<string> #include< ...

  3. 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 ...

  4. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  5. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  6. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  7. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  8. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  9. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

随机推荐

  1. Dokcerfile部署webpy,安装imagehash库并运行py脚本获取图片dhash值

    Dockerfile FROM lmurawsk/python2.7 RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple image ...

  2. PAT 1067 Sort with Swap[难]

    1067 Sort with Swap(0,*) (25)(25 分) Given any permutation of the numbers {0, 1, 2,..., N-1}, it is e ...

  3. Django之分页功能

    Django提供了一个新的类来帮助你管理分页数据,这个类存放在django/core/paginator.py.它可以接收列表.元组或其它可迭代的对象. 基本语法 class Paginator(ob ...

  4. html03

    快速开发指南:1.新建页面之后,编写外部样式表文件,引入到HTML页面中2.用浏览器打开页面->F12->sources->打开css文件 右边编写样式,左边查看效果3.样式编写完成 ...

  5. VMware coding Challenge:Date of Weekday

    这道题再次证明了这种细节的题目,画个图容易搞清楚 import java.util.Scanner; public class Solution2 { static int DateOfWeekday ...

  6. Javascript-逻辑判断或(&&)练习

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. c#中ref和out使用及区别

    在c#中,使用方法获得返回值时,只能获取一个返回值.当使用ref和out关键字后,可以获取多个返回值. MSDN对ref和out关键字的说明如下: ref 关键字: 使参数按引用传递.其效果是,当控制 ...

  8. STA分析(二) multi_cycle and false

    multicycle path:当FF之间的组合逻辑path propagate delay大于一个时钟cycle时,这条combinational path能被称为multicycle path. ...

  9. lnmp1.4 安装php fileinfo扩展 方法

    第一步:在lnmp1.4找到php安装的版本 使用命令 tar   -jxvf   php-7.1.7.tar.bz2 解压 第二步: 在解压的php-7.1.7文件夹里找到fileinfo文件夹,然 ...

  10. Linux基础命令---cmp

    cmp 用字节的方式,比较两个文件是否存在差异,但是不保存运算结果.Cmp指令只会根据结果设置相关的标志位,这个指令之后往往会跟着一个条件跳转指令. 此命令的适用范围:RedHat.RHEL.Ubun ...