Codeforces Round #251 (Div. 2) D. 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 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.

Sample test(s)
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



三分A数组的最小值,比赛的时候被系统搞掉了,把最后三分出的最小值l,取[l-5,l+5]的最小操作数,就过了。。。。。。。。。

#include <stdio.h>
#include <vector>
#include <string>
#include <queue>
#include <string.h>
#include <algorithm>
using namespace std;

#define rep(i,s,t) for(int i=s;i<t;i++)
#define red(i,s,t) for(int i=s-1;i>=t;i--)
#define clr(a,v) memset(a,v,sizeof a)
#define min(a,b) (a<b?a:b)

inline int input(){
    int ret=0;bool isN=0;
    char c=getchar();
    while(c<'0' || c>'9'){
        if(c=='-') isN=1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        ret=ret*10+c-'0';
        c=getchar();
    }
    return isN?-ret:ret;
}

int n,m;
int a[100005],b[100005];
inline void In(){
    n=input(),m=input();
    rep(i,0,n) a[i]=input();
    rep(j,0,m) b[j]=input();
    sort(a,a+n);
    sort(b,b+m);
}

inline long long get(int x){
    long long ans=0;
    rep(i,0,n){
        if(x>a[i]) ans+=(long long)(x-a[i]);
        else break;
    }
    red(i,m,0){
        if(b[i]>x) ans+=(long long)(b[i]-x);
        else break;
    }
    return ans;
}

inline void Work(){
    if(a[0]>=b[m-1]){
        puts("0");
    }
    else{
        int l=1,r=2000000000,cnt=0,mid,mmid;
        while(cnt<=100){
            mid=l+(r-l)/2;
            mmid=mid+(r-mid)/2;
            long long res=get(mid);
            long long rres=get(mmid);
            if(res<rres) r=mmid;
            else l=mid;
            cnt++;
        }
        long long ans=get(l);
        cnt=1;
        while(cnt<=5){
            ans=min(ans,get(l+cnt));cnt++;
        }
        cnt=1;
        while(cnt<=5){
            ans=min(ans,get(l-cnt));cnt++;
        }
        printf("%I64d\n",ans);
    }
}
int main(){
    In();
    Work();
    return 0;
}


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值