結果

問題 No.1467 Selling Cars
ユーザー KoDKoD
提出日時 2021-04-03 20:28:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,613 ms / 4,000 ms
コード長 3,143 bytes
コンパイル時間 2,350 ms
コンパイル使用メモリ 211,660 KB
実行使用メモリ 4,944 KB
最終ジャッジ日時 2023-08-26 16:14:31
合計ジャッジ時間 27,380 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 576 ms
4,944 KB
testcase_01 AC 518 ms
4,820 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1,600 ms
4,496 KB
testcase_04 AC 1,596 ms
4,472 KB
testcase_05 AC 1,584 ms
4,432 KB
testcase_06 AC 1,613 ms
4,512 KB
testcase_07 AC 1,579 ms
4,500 KB
testcase_08 AC 1,592 ms
4,456 KB
testcase_09 AC 3 ms
4,604 KB
testcase_10 AC 3 ms
4,576 KB
testcase_11 AC 1,023 ms
4,380 KB
testcase_12 AC 1,303 ms
4,380 KB
testcase_13 AC 1,145 ms
4,380 KB
testcase_14 AC 163 ms
4,380 KB
testcase_15 AC 536 ms
4,492 KB
testcase_16 AC 307 ms
4,380 KB
testcase_17 AC 413 ms
4,380 KB
testcase_18 AC 1,167 ms
4,428 KB
testcase_19 AC 550 ms
4,796 KB
testcase_20 AC 471 ms
4,884 KB
testcase_21 AC 997 ms
4,620 KB
testcase_22 AC 532 ms
4,736 KB
testcase_23 AC 493 ms
4,712 KB
testcase_24 AC 451 ms
4,928 KB
testcase_25 AC 31 ms
4,380 KB
testcase_26 AC 215 ms
4,768 KB
testcase_27 AC 176 ms
4,384 KB
testcase_28 AC 310 ms
4,532 KB
testcase_29 AC 232 ms
4,380 KB
testcase_30 AC 222 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept {
            return itr != other.itr;
        }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr rep(const usize first, const usize last) noexcept
        : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class T, T Div = 2>
constexpr T INFTY = std::numeric_limits<T>::max() / Div;

template <class T> using Vec = std::vector<T>;

void G_main() {
    usize M, N;
    std::cin >> M >> N;
    Vec<i64> A(M), B(N);
    for (auto& x : A) {
        std::cin >> x;
    }
    for (auto& x : B) {
        std::cin >> x;
    }
    const auto cost = [&](const usize i, const usize j) {
        return std::abs(A[i] - B[j]);
    };
    std::sort(A.begin(), A.end());
    std::sort(B.begin(), B.end());
    Vec<usize> best(M);
    for (const auto i : rep(0, M)) {
        for (const auto j : rep(1, N)) {
            if (cost(i, best[i]) > cost(i, j)) {
                best[i] = j;
            }
        }
    }
    for (const auto k : rep(1, M + 1)) {
        Vec<std::deque<usize>> used(N);
        for (const auto i : rep(0, M)) {
            const auto b = best[i];
            if (used[b].size() < k) {
                used[b].push_back(i);
                continue;
            }
            usize l = b, r = b;
            while (l > 0 && used[l - 1].size() == k) {
                l -= 1;
            }
            while (r < N && used[r].size() == k) {
                r += 1;
            }
            const auto cost_l = [&] {
                if (l == 0) {
                    return INFTY<i64>;
                }
                i64 ret = cost(i, r - 1);
                for (const auto j : rep(l, r)) {
                    ret +=
                        cost(used[j].front(), j - 1) - cost(used[j].front(), j);
                }
                return ret;
            }();
            if (r != N && cost_l > cost(i, r)) {
                used[r].push_back(i);
            } else {
                for (const auto j : rep(l, r)) {
                    used[j - 1].push_back(used[j].front());
                    used[j].pop_front();
                }
                used[r - 1].push_back(i);
            }
        }
        i64 ans = 0;
        for (const auto j : rep(0, N)) {
            for (const auto i : used[j]) {
                ans += cost(i, j);
            }
        }
        std::cout << ans << '\n';
    }
    return;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    G_main();
    return 0;
}
0