7 solutions

  • 0
    @ 2022-10-13 18:29:02
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    #define x first
    #define y second
    #define LL long long
    typedef pair<int,int> PII;
    int dx[] = {-1, 1, 0, 0};
    int dy[] = {0, 0, -1, 1};
    const int N = 1e6 + 10, MOD = 1e9 + 10;
    int a[N], b[N];
    int n, m, res;
    
    int ef1(int x)
    {
        int l = 1, r = n;
        while( r > l )
        {
            int mid = l + r >> 1;
            if( a[mid] >= x )    r = mid;
            else    l = mid + 1;
        }
        return a[l];
    }
    
    int ef2(int x )
    {
        int l = 1, r = n;
        while( r > l )
        {
            int mid = l + r + 1 >> 1;
            if( a[mid] <= x )   l = mid;
            else    r = mid - 1;
        }
        return a[l];
    }
    void solve()
    {
        
        cin >> n >> m;
        for(int i = 1; i <= n; i ++ )   cin >> a[i];
        for(int i = 1; i <= m; i ++ )   cin >> b[i];
        sort(a+1, a+1+n);
        a[0] = -1, a[m+1] = 1e9;
        for(int i = 1; i <= m; i ++ )
            res += min(abs(b[i]-ef1(b[i])), abs(b[i]-ef2(b[i])));
        cout << res;
    }
    signed main()
    {
        solve();
        return 0;
    }
    

    Information

    ID
    280
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    8
    Tags
    # Submissions
    266
    Accepted
    51
    Uploaded By