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

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

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

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll long long
    #define mod 1000000007
    #define inf 999999999
    //#pragma comment(linker, "/STACK:102400000,102400000")
    int scan()
    {
        int res = 0 , ch ;
        while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
        {
            if( ch == EOF ) return 1 << 30 ;
        }
        res = ch - '0' ;
        while( ( ch = getchar() ) >= '0' && ch <= '9' )
            res = res * 10 + ( ch - '0' ) ;
        return res ;
    }
    ll a[100010];
    ll b[100010];
    ll check(ll x,ll y,ll cha)
    {
        ll sum=0;
        for(ll i=0;i<x;i++)
        if(cha>a[i])
        sum+=(cha-a[i]);
        for(ll i=0;i<y;i++)
        if(cha<b[i])
        sum+=b[i]-cha;
        return sum;
    }
    int main()
    {
        ll x,y,z,i,t;
        scanf("%I64d%I64d",&x,&y);
        ll maxx=0;
        ll minn=1000000010;
        for(i=0;i<x;i++)
        {
            scanf("%I64d",&a[i]);
            minn=min(minn,a[i]);
        }
        for(i=0;i<y;i++)
        {
            scanf("%I64d",&b[i]);
            maxx=max(maxx,b[i]);
        }
        if(minn>=maxx)
        {
            printf("0
    ");
            return 0;
        }
        ll st=minn;
        ll en=maxx;
        ll mid,midd;
        ll ans=min(check(x,y,minn),check(x,y,maxx));
        while(st<en)
        {
            mid=(st+en)/2;
            midd=(mid+en)/2;
            ll  checkmid=check(x,y,mid);
            ll checkmidd=check(x,y,midd);
            if(checkmid>checkmidd)
            st=mid;
            else
            en=midd;
            ans=min(ans,min(checkmid,checkmidd));
        }
        printf("%I64d
    ",ans);
        return 0;
    }
  • 相关阅读:
    Windows Phone 7(WP7)开发 自订磁贴(深度链接)
    Windows Phone 7(WP7)开发 在ViewModel中使用NavigationService
    Windows Phone 7(WP7)开发 显示长文本(高度大于2000px)
    类属性生成器(小程序)
    Windows Phone 7(WP7)开发 ListBox的分页加载
    Windows Phone 7(WP7)开发工具 查看独立存储空间中数据库内容
    Windows Phone 7(WP7)开发 获取网络状态
    发布一个XNA写的小雷电源码
    用python来个百度关键词刷排名脚本
    win7下 VirtualBox虚拟机开机后台自启动
  • 原文地址:https://www.cnblogs.com/jhz033/p/5478484.html
Copyright © 2011-2022 走看看