• CodeForces 439D


    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 arraysa and b by their father. The arraya 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 arrayb.

    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 containn space-separated integers representing content of the arraya (1 ≤ ai ≤ 109). The third line will contain m space-separated integers representing content of the arrayb (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
    
    Note

    In example 1, you can increase a1 by1 and decrease b2 by1 and then again decrease b2 by1. 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 are3.

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


    思路:三分寻找答案

    #include <bits/stdc++.h>
    #define INF 0x3fffffff
    typedef long long LL;
    const LL inf=(((LL)1)<<61)+5;
    
    using namespace std;
    LL midmid,mid,n,m;
    LL ans;
    LL a[100007],b[100007];
    LL slove(LL num)
    {
        LL sum=0;
        for(int i=0;i<n;i++)
        {
            if(num>a[i]) sum+=num-a[i];
        }
        for(int i=0;i<m;i++)
        {
            if(num<b[i]) sum+=b[i]-num;
        }
        return sum;
    }
    
    int main()
    {
        scanf("%I64d%I64d",&n,&m);
        LL mmin=INF,mmax=0;
        for(int i=0;i<n;i++) {scanf("%I64d",&a[i]);mmin=min(mmin,a[i]);}
        for(int i=0;i<m;i++) {scanf("%I64d",&b[i]);mmax=max(mmax,b[i]);}
        sort(a,a+n);sort(b,b+m);
        LL l=0,r=INF;
        if(mmin>mmax) ans=0;
        else{
        ans=inf;
        while(l<=r){
            mid=(l+r)>>1;
            midmid=(r+mid)>>1;
            LL ans1=slove(mid);
            LL ans2=slove(midmid);
            if(ans1<ans2) {r=midmid-1;ans=min(ans,ans1);}
            else {l=mid+1;ans=min(ans,ans2);}
        }}
        printf("%I64d
    ",ans);
        return 0;
    }
    


     

    思路二:

      排序,第m个为目标数(看了大神的)

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int maxN=100005;
    int a[maxN],b[maxN],c[maxN*2];
    
    int main()
    {
        int n,m;
        long long ans=0;
    
        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[i+n]=b[i];
        }
        sort(c,c+n+m);
        int t=c[m];
    
        for(int i=0; i<n; i++)
            ans+=max(0,t-a[i]);
        for(int i=0; i<m; i++)
            ans+=max(0,b[i]-t);
        printf("%I64d
    ",ans);
        return 0;
    }


     

  • 相关阅读:
    Git 使用juju
    svn Q&A
    ubuntu 常用命令
    java中set的交集、差集、并集的简单实现
    java 读写JSON(一)
    java类型转化之SimpleDateFormat-时间转化
    maven 基本常识以及命令
    maven遇到的问题
    java编程思想,对象导论
    条目5:避免创建不必要的对象
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254445.html
Copyright © 2020-2023  润新知