結果

問題 No.2157 崖
ユーザー ruthenruthen
提出日時 2022-12-10 17:40:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,108 ms / 6,000 ms
コード長 1,594 bytes
コンパイル時間 2,778 ms
コンパイル使用メモリ 212,948 KB
実行使用メモリ 19,968 KB
最終ジャッジ日時 2024-04-23 00:40:13
合計ジャッジ時間 13,983 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 3 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 3 ms
6,944 KB
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 753 ms
14,208 KB
testcase_14 AC 989 ms
18,560 KB
testcase_15 AC 705 ms
13,952 KB
testcase_16 AC 713 ms
14,336 KB
testcase_17 AC 840 ms
16,256 KB
testcase_18 AC 825 ms
16,000 KB
testcase_19 AC 909 ms
16,768 KB
testcase_20 AC 1,108 ms
19,968 KB
testcase_21 AC 1,092 ms
19,840 KB
testcase_22 AC 901 ms
15,488 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifdef _RUTHEN
#include <debug.hpp>
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, M;
    cin >> N >> M;
    V<V<ll>> D(N, V<ll>(M));
    rep(i, N) {
        rep(j, M) cin >> D[i][j];
        sort(D[i].begin(), D[i].end());
    }
    const ll INF = 1LL << 60;
    V<ll> dp(M, 0);
    rep(i, N - 1) {
        V<V<pair<int, ll>>> G(M + 1);
        rep(j, M) {
            int lb = lower_bound(D[i + 1].begin(), D[i + 1].end(), D[i][j]) - D[i + 1].begin();
            G[lb].push_back({1, dp[j]});
            int ub = lower_bound(D[i + 1].begin(), D[i + 1].end(), D[i][j] + dp[j]) - D[i + 1].begin();
            G[ub].push_back({2, dp[j]});
            G[ub].push_back({3, -D[i][j]});
        }
        show(G);
        V<ll> np(M, INF);
        multiset<ll> S1, S2;
        S1.insert(INF);
        S2.insert(INF);
        rep(j, M) {
            for (auto [t, v] : G[j]) {
                if (t == 1) {
                    S1.insert(v);
                } else if (t == 2) {
                    S1.extract(v);
                } else {
                    S2.insert(v);
                }
            }
            np[j] = min({*S1.begin(), D[i + 1][j] + *S2.begin(), INF});
            show(S1, S2);
        }
        swap(dp, np);
        show(dp);
    }
    ll ans = *min_element(dp.begin(), dp.end());
    if (ans >= INF) ans = -1;
    cout << ans << '\n';
    return 0;
}
0