結果

問題 No.2114 01 Matching
ユーザー 👑 rin204rin204
提出日時 2022-10-29 14:15:39
言語 C++23(draft)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 353 ms / 5,000 ms
コード長 1,963 bytes
コンパイル時間 6,128 ms
コンパイル使用メモリ 295,620 KB
実行使用メモリ 47,144 KB
最終ジャッジ日時 2023-08-16 02:52:10
合計ジャッジ時間 17,737 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 164 ms
14,532 KB
testcase_03 AC 52 ms
12,576 KB
testcase_04 AC 78 ms
16,252 KB
testcase_05 AC 82 ms
8,244 KB
testcase_06 AC 140 ms
13,440 KB
testcase_07 AC 51 ms
5,016 KB
testcase_08 AC 251 ms
14,184 KB
testcase_09 AC 185 ms
6,936 KB
testcase_10 AC 169 ms
10,520 KB
testcase_11 AC 348 ms
47,056 KB
testcase_12 AC 344 ms
47,052 KB
testcase_13 AC 232 ms
22,212 KB
testcase_14 AC 317 ms
24,516 KB
testcase_15 AC 316 ms
24,356 KB
testcase_16 AC 304 ms
24,184 KB
testcase_17 AC 139 ms
13,380 KB
testcase_18 AC 152 ms
13,628 KB
testcase_19 AC 219 ms
22,456 KB
testcase_20 AC 334 ms
47,052 KB
testcase_21 AC 264 ms
22,508 KB
testcase_22 AC 139 ms
6,756 KB
testcase_23 AC 194 ms
14,748 KB
testcase_24 AC 56 ms
4,580 KB
testcase_25 AC 63 ms
7,396 KB
testcase_26 AC 345 ms
47,052 KB
testcase_27 AC 336 ms
47,112 KB
testcase_28 AC 172 ms
27,564 KB
testcase_29 AC 350 ms
47,144 KB
testcase_30 AC 52 ms
5,312 KB
testcase_31 AC 353 ms
47,108 KB
testcase_32 AC 248 ms
9,348 KB
testcase_33 AC 172 ms
14,592 KB
testcase_34 AC 53 ms
5,184 KB
testcase_35 AC 122 ms
21,148 KB
testcase_36 AC 351 ms
47,072 KB
testcase_37 AC 205 ms
8,848 KB
testcase_38 AC 54 ms
5,280 KB
testcase_39 AC 120 ms
6,040 KB
testcase_40 AC 111 ms
20,856 KB
testcase_41 AC 223 ms
7,376 KB
testcase_42 AC 178 ms
9,020 KB
testcase_43 AC 135 ms
13,352 KB
testcase_44 AC 293 ms
23,548 KB
testcase_45 AC 175 ms
47,124 KB
testcase_46 AC 73 ms
4,872 KB
testcase_47 AC 338 ms
47,120 KB
testcase_48 AC 347 ms
47,024 KB
testcase_49 AC 219 ms
21,688 KB
testcase_50 AC 89 ms
5,000 KB
testcase_51 AC 48 ms
5,172 KB
testcase_52 AC 314 ms
24,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include<atcoder/lazysegtree>
using namespace std;
using namespace atcoder;

using S = long long;

S op(S l, S r){
    if(l <= r) return l;
    else return r;
}

S e(){
    return 1LL << 60;
}

using F = long long;

S mapping(F l, S r){
    return l + r;
}

F composition(F l, F r){
    return l + r;
}

F id(){
    return 0;
}

long long f(vector<int> &R, vector<int> &B){
    vector<pair<int, int>> event;
    for(auto r:R) event.push_back({r, 0});
    for(auto b:B) event.push_back({b, 1});
    sort(event.begin(), event.end());

    int n = R.size();
    int m = B.size();
    int z = n;
    lazy_segtree<S, op, e, F, mapping, composition, id> seg(n + m + 1);
    seg.set(z, 0);

    for(auto tmp:event){
        int x = tmp.first;
        if(tmp.second == 0){
            seg.apply(0, z, x);
            seg.apply(z, n + m + 1, -x);
            z--;
        }
        else{
            auto tmp = seg.get(z);
            z++;
            seg.apply(0, z, -x);
            seg.apply(z, n + m + 1, x);
            if(tmp < seg.get(z)){
                seg.set(z, tmp);
            }
        }
    }

    return seg.get(z);
}

void solve(){
    int n, m, k;
    cin >> n >> m >> k;
    map<int, vector<int>> B, R;
    int x;
    for(int i = 0; i < n; i++){
        cin >> x;
        B[x % k].push_back(x / k);
    }
    for(int i = 0; i < m; i++){
        cin >> x;
        R[x % k].push_back(x / k);
    }

    if(n > m){
        swap(n, m);
        swap(B, R);
    }

    long long ans = 0;
    
    for(auto tmp:B){
        int k = tmp.first;
        if(!R.count(k) || (B[k].size() > R[k].size())){
            cout << "-1\n";
            return;
        }
        ans += f(B[k], R[k]);
    }
    cout << ans << "\n";
    
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t;
    t = 1;
    // cin >> t;
    while(t--) solve();
}





0