• Hihocoder-1514 偶像的条件


    解题思路:

    其实就是随意搞下就行。

    求|a - b| + |b - c| + |c - a|的最小值

    显然枚举a,然后二分整个{b}找到离a最近的b,再二分整个{c}找到离a最近的c和离b最近的c

    比较一下谁最近,就可以O(nlogn)搞出来了。


    代码:

    #include <set>
    #include <map>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <bitset>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    
    const int maxn = 1e5 + 5;
    int a[maxn], b[maxn], c[maxn];
    
    inline long long Abs(long long x) {
        return (x >= 0 ? x : -x);
    }
    int main() {
        ios::sync_with_stdio(false); cin.tie(0);
        int n, m, l; cin >> n >> m >> l;
        for ( int i = 0; i < n; ++i ) cin >> a[i];
        for ( int i = 0; i < m; ++i ) cin >> b[i];
        for ( int i = 0; i < l; ++i ) cin >> c[i];
        sort(a, a + n); sort(b, b + m); sort(c, c + l);
    
        long long ans = 1e18 + 5;
        for ( int i = 0; i < n; ++i ) {
            int pos_b = lower_bound(b, b + m, a[i]) - b;
    
            int pos_c = lower_bound(c, c + l, a[i]) - c;
            ans = min(ans, Abs(a[i] - b[pos_b]) + Abs(b[pos_b] - c[pos_c]) + Abs(c[pos_c] - a[i]));
            if ( pos_c > 0 ) ans = min(ans, Abs(a[i] - b[pos_b]) + Abs(b[pos_b] - c[pos_c-1]) + Abs(c[pos_c-1] - a[i]));
    
            pos_c = lower_bound(c, c + l, b[pos_b]) - c;
            ans = min(ans, Abs(a[i] - b[pos_b]) + Abs(b[pos_b] - c[pos_c]) + Abs(c[pos_c] - a[i]));
            if ( pos_c > 0 ) ans = min(ans, Abs(a[i] - b[pos_b]) + Abs(b[pos_b] - c[pos_c-1]) + Abs(c[pos_c-1] - a[i]));
    
            if ( pos_b > 0 ) {
                pos_c = lower_bound(c, c + l, b[pos_b-1]) - c;
                ans = min(ans, Abs(a[i] - b[pos_b-1]) + Abs(b[pos_b-1] - c[pos_c]) + Abs(c[pos_c] - a[i]));
                if ( pos_c > 0 ) ans = min(ans, Abs(a[i] - b[pos_b-1]) + Abs(b[pos_b-1] - c[pos_c-1]) + Abs(c[pos_c-1] - a[i]));
            }
        }
        cout << ans << endl;
        return 0;
    }


  • 相关阅读:
    wmware虚拟机的克隆
    解决SecureCRT无法用非root账号登录ssh
    Docker容器操作
    Docker镜像操作
    Docker的安装和启动
    linux安装tomcat
    POJ 2456 Aggressive cows ( 二分搜索)
    POJ 1064 Cable master (二分查找)
    2008 APAC local onsites C Millionaire (动态规划,离散化思想)
    贿赂囚犯 Bribe the prisoners ( 动态规划+剪枝)
  • 原文地址:https://www.cnblogs.com/wiklvrain/p/8179325.html
Copyright © 2020-2023  润新知