Codeforces 439D Devu and his Brother【思维+三分查找】

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 b by 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 n, m (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.

Examples
Input
2 2
2 3
3 5
Output
3
Input
3 2
1 2 3
3 4
Output
4
Input
3 2
4 5 6
1 2
Output
0
Note

In example 1, you can increase a1 by 1 and decrease b2 by 1 and then again decrease b2 by 1. Now array a will be [3; 3] and array b will also be [3; 3]. Here minimum element of a is at least as large as maximum element of b. So minimum number of operations needed to satisfy Devu's condition are 3.

In example 3, you don't need to do any operation, Devu's condition is already satisfied. 


题目大意:


现在有两个人,第一个人有N个数,第二个人有M个数。

现在一次操作可以使得任意一个人的任意一个数字减小1或者增加1.

现在第一个人希望他最小的数都大于等于第二个人最大的数,问最小操作数量。


思路:


对应假设我们找到了一个数tmp,使得这个数是第一个人最终的最小的数,那么我们对应需要总共操作:

Ans=Σtmp-a【i】(a【i】<tmp)+Σb【i】-tmp(b【i】>tmp);

很显然对于最优解X来讲,无论当X+1作为这个数的时候,还是X-1作为这个数的时候,都肯定要比这个最优解进行的操作数量多。

同理,X+2,X-2会更多。


那么这里有一个三分性质,我们三分答案进行check即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll __int64
ll n,m;
ll a[150000];
ll b[150000];
ll c[305000];
ll Slove(ll mid)
{
    ll ans=0;
    for(ll i=1;i<=n;i++)
    {
        if(a[i]<mid)ans+=mid-a[i];
    }
    for(ll i=1;i<=m;i++)
    {
        if(b[i]>mid)ans+=b[i]-mid;
    }
    return ans;
}
int Sanfen(int l,int r)
{
    int lm,rm;
    while(l<r-1)
    {
        lm=(l+r)/2;
        rm=(lm+r)/2;
        if(Slove(c[lm])>Slove(c[rm]))
        {
            l=lm;
        }
        else r=rm;
    }
    return Slove(c[l])>Slove(c[r])?r:l;
}
int main()
{
    while(~scanf("%I64d%I64d",&n,&m))
    {
        ll cnt=0;
        for(ll i=1;i<=n;i++)scanf("%I64d",&a[i]);
        for(ll i=1;i<=m;i++)scanf("%I64d",&b[i]);
        sort(a+1,a+1+n);
        sort(b+1,b+1+m);
        for(ll i=1;i<=n;i++)c[++cnt]=a[i];
        for(ll i=1;i<=m;i++)c[++cnt]=b[i];
        sort(c+1,c+1+n+m);
        int pos=Sanfen(1,n+m);
        printf("%I64d\n",Slove(c[pos]));
    }
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值