結果

問題 No.2114 01 Matching
ユーザー suisensuisen
提出日時 2023-01-25 20:22:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 710 ms / 5,000 ms
コード長 2,740 bytes
コンパイル時間 1,557 ms
コンパイル使用メモリ 125,216 KB
実行使用メモリ 53,632 KB
最終ジャッジ日時 2024-04-27 05:12:28
合計ジャッジ時間 22,341 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 332 ms
16,892 KB
testcase_03 AC 109 ms
13,952 KB
testcase_04 AC 164 ms
18,304 KB
testcase_05 AC 86 ms
9,556 KB
testcase_06 AC 339 ms
15,764 KB
testcase_07 AC 318 ms
10,824 KB
testcase_08 AC 280 ms
16,004 KB
testcase_09 AC 204 ms
7,756 KB
testcase_10 AC 319 ms
10,912 KB
testcase_11 AC 710 ms
53,504 KB
testcase_12 AC 707 ms
53,504 KB
testcase_13 AC 257 ms
25,492 KB
testcase_14 AC 355 ms
28,200 KB
testcase_15 AC 360 ms
28,192 KB
testcase_16 AC 351 ms
28,648 KB
testcase_17 AC 336 ms
15,864 KB
testcase_18 AC 172 ms
15,588 KB
testcase_19 AC 242 ms
24,760 KB
testcase_20 AC 691 ms
53,632 KB
testcase_21 AC 286 ms
26,796 KB
testcase_22 AC 157 ms
7,552 KB
testcase_23 AC 216 ms
17,308 KB
testcase_24 AC 63 ms
6,944 KB
testcase_25 AC 70 ms
8,616 KB
testcase_26 AC 677 ms
53,504 KB
testcase_27 AC 694 ms
53,376 KB
testcase_28 AC 329 ms
31,104 KB
testcase_29 AC 697 ms
53,504 KB
testcase_30 AC 339 ms
15,580 KB
testcase_31 AC 699 ms
53,504 KB
testcase_32 AC 280 ms
10,188 KB
testcase_33 AC 329 ms
16,964 KB
testcase_34 AC 333 ms
11,020 KB
testcase_35 AC 241 ms
23,808 KB
testcase_36 AC 697 ms
53,504 KB
testcase_37 AC 216 ms
10,008 KB
testcase_38 AC 321 ms
10,736 KB
testcase_39 AC 123 ms
6,944 KB
testcase_40 AC 228 ms
23,296 KB
testcase_41 AC 245 ms
8,192 KB
testcase_42 AC 201 ms
10,612 KB
testcase_43 AC 338 ms
15,932 KB
testcase_44 AC 336 ms
27,588 KB
testcase_45 AC 379 ms
53,248 KB
testcase_46 AC 82 ms
6,940 KB
testcase_47 AC 694 ms
53,376 KB
testcase_48 AC 709 ms
53,376 KB
testcase_49 AC 243 ms
24,656 KB
testcase_50 AC 96 ms
6,940 KB
testcase_51 AC 333 ms
16,972 KB
testcase_52 AC 356 ms
28,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits>
#include <map>
#include <vector>

#include <atcoder/lazysegtree>

constexpr long long inf = std::numeric_limits<long long>::max() / 2;

long long op(long long x, long long y) {
    return std::min(x, y);
}
long long e() {
    return inf;
}
long long mapping(long long f, long long x) {
    return f + x;
}
long long composition(long long f, long long g) {
    return f + g;
}
long long id() {
    return 0;
}

long long solve(std::vector<int> a, std::vector<int> b) {
    if (a.size() > b.size()) std::swap(a, b);
    std::sort(a.begin(), a.end());
    std::sort(b.begin(), b.end());

    const int n = a.size(), m = b.size();

    std::vector<std::pair<int, bool>> v;
    for (int i = 0, j = 0; i < n or j < m;) {
        if (i == n or (j < m and b[j] < a[i])) {
            v.emplace_back(b[j++], false);
        } else {
            v.emplace_back(a[i++], true);
        }
    }
    const int l = n + m + 1;
    int z = n;
    std::vector<long long> init(l, inf);
    init[z] = 0;
    atcoder::lazy_segtree<long long, op, e, long long, mapping, composition, id> seg(init);
    for (auto [x, c] : v) {
        if (c) {
            seg.apply(z, l, -x);
            seg.apply(0, z, +x);
            --z;
        } else {
            long long t = seg.get(z);
            seg.apply(0, z + 1, -x);
            seg.apply(z + 1, l, +x);
            ++z;
            seg.set(z, std::min(t, seg.get(z)));
        }
        // for (int i = 0; i < l; ++i) {
        //     int j = i - z;
        //     long long t = seg.get(i);
        //     std::cerr << std::setw(2) << j << ":";
        //     if (t > inf / 2) std::cerr << "inf";
        //     else std::cerr << std::setw(3) << t;
        //     if (i == l - 1) {
        //         std::cerr << '\n';
        //     } else {
        //         std::cerr << ", ";
        //     }
        // }
    }
    return seg.get(z);
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, m, k;
    std::cin >> n >> m >> k;

    std::map<int, std::array<std::vector<int>, 2>> mp;
    for (int i = 0; i < n; ++i) {
        int e;
        std::cin >> e;
        int q = e / k, r = e % k;
        mp[r][0].push_back(q);
    }
    for (int i = 0; i < m; ++i) {
        int e;
        std::cin >> e;
        int q = e / k, r = e % k;
        mp[r][1].push_back(q);
    }
    int num = 0;
    long long ans = 0;
    for (auto [_, qs] : mp) {
        auto [b, r] = qs;
        num += std::min(b.size(), r.size());
        ans += solve(b, r);
    }
    if (num != std::min(n, m)) {
        std::cout << -1 << std::endl;
    } else {
        std::cout << ans << std::endl;
    }
    return 0;
}

0