zoukankan      html  css  js  c++  java
  • 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 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.

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


    贪心问题,代码不长,好看懂



    AC代码例如以下:

    #include<iostream>
    #include<string>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<stack>
    #include<iomanip>
    using namespace std;
    #define ll long long
    #define M 100005
    
    bool cmp(int a,int b)
    {
        return a>b;
    }
    
    int min(int a,int b)
    {
        return a<b?a:b;
    }
    
    int main()
    {
        int n,m,i;
        int a[M],b[M];
        cin>>n>>m;
        for(i=0;i<n;i++)
            cin>>a[i];
        for(i=0;i<m;i++)
            cin>>b[i];
        sort(a,a+n);
        sort(b,b+m,cmp);
        n=min(n,m);
        ll sum=0;
        for(i=0;i<n;i++)
        {
            if(b[i]>a[i])
                sum+=(ll)(b[i]-a[i]);
        }
        cout<<sum<<endl;
        return 0;
    }



  • 相关阅读:
    2019-4-16-C#-使用反射获取私有属性的方法
    2019-5-21-C#-命令行如何静默调用-del-删除文件
    2019-6-14-WPF-shows-that-some-windows-in-multithreading-will-be-locked-in-the-PenThreadWorker-constr...
    2019-9-11-完整的-P2P-应用需要包含哪些功能
    2019-3-1-VisualStudio-扩展开发-获得输出窗口内容
    2018-10-19-C#-序列类为-xml-可以使用的特性大全
    2018-8-10-win10-uwp-手把手教你使用-asp-dotnet-core-做-cs-程序
    2018-10-31-win10-uwp-使用-asp-dotnet-core-做图床服务器客户端
    2018-8-10-docfx-做一个和微软一样的文档平台
    linux分区方案
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3956021.html
Copyright © 2011-2022 走看看