結果

問題 No.1767 BLUE to RED
ユーザー milanis48663220milanis48663220
提出日時 2021-11-26 22:42:28
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 2,779 bytes
コンパイル時間 1,422 ms
コンパイル使用メモリ 132,520 KB
実行使用メモリ 24,256 KB
最終ジャッジ日時 2023-09-12 05:17:43
合計ジャッジ時間 6,362 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 129 ms
13,100 KB
testcase_10 AC 170 ms
13,692 KB
testcase_11 AC 140 ms
13,716 KB
testcase_12 AC 132 ms
14,052 KB
testcase_13 AC 188 ms
15,180 KB
testcase_14 AC 260 ms
23,724 KB
testcase_15 AC 263 ms
23,156 KB
testcase_16 AC 271 ms
24,256 KB
testcase_17 AC 267 ms
23,636 KB
testcase_18 AC 267 ms
22,684 KB
testcase_19 AC 263 ms
23,796 KB
testcase_20 AC 264 ms
23,896 KB
testcase_21 AC 263 ms
22,732 KB
testcase_22 AC 268 ms
23,552 KB
testcase_23 AC 264 ms
23,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

using P = pair<int, int>;
int RED = 0, BLUE = 1;


struct UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -1) {}
    bool unionSet(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
        if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

using T = tuple<ll, int, int>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m; cin >> n >> m;
    vector<P> p;
    for(int i = 0; i < n; i++){
        int a; cin >> a;
        p.push_back(P(a, RED)); 
    }
    for(int i = 0; i < m; i++){
        int b; cin >> b;
        p.push_back(P(b, BLUE)); 
    }
    sort(p.begin(), p.end());
    int root = 0;
    priority_queue<T, vector<T>, greater<T>> t;
    for(int i = 0; i < n+m; i++){
        auto [x, col] = p[i];
        int idx = i+1;
        if(col == RED) t.push(T(0, root, idx));
        if(i > 0){
            auto [x_pre, _] = p[i-1];
            t.push(T(x-x_pre, idx-1, idx));
        }
    }
    ll ans = 0;
    UnionFind uf(n+m+1);
    while(uf.size(root) != n+m+1){
        auto [w, i, j] = t.top(); t.pop();
        if(uf.findSet(i, j)) continue;
        // if(i != 0) cout << p[i-1].first << ' ' << p[j-1].first << endl;
        ans += w;
        uf.unionSet(i, j);
    }
    cout << ans << endl;
}
0