結果

問題 No.2157 崖
ユーザー wanuiwanui
提出日時 2022-12-09 22:52:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 639 ms / 6,000 ms
コード長 2,663 bytes
コンパイル時間 2,325 ms
コンパイル使用メモリ 206,684 KB
実行使用メモリ 21,120 KB
最終ジャッジ日時 2023-08-05 00:49:14
合計ジャッジ時間 10,128 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 476 ms
15,664 KB
testcase_14 AC 574 ms
20,940 KB
testcase_15 AC 581 ms
17,728 KB
testcase_16 AC 582 ms
17,772 KB
testcase_17 AC 488 ms
19,820 KB
testcase_18 AC 478 ms
19,824 KB
testcase_19 AC 623 ms
17,732 KB
testcase_20 AC 639 ms
20,564 KB
testcase_21 AC 636 ms
21,120 KB
testcase_22 AC 464 ms
19,816 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 5 ms
18,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;
void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);}
void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);}
void perr0(){}; template<typename H,typename... T> void perr0(H h,T... t){cerr<<h;perr0(t...);}
void perr(){perr0("\n");}; template<typename H,typename... T>void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);}
void ioinit() { cout<<fixed<<setprecision(15); cerr<<fixed<<setprecision(6); ios_base::sync_with_stdio(0); cin.tie(0); }
#define debug1(a) { cerr<<#a<<":"<<a<<endl; }
#define debug2(a,b) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<endl; }
#define debug3(a,b,c) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<" "<<#c<<":"<<c<<endl; }
#define debug4(a,b,c,d) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<" "<<#c<<":"<<c<<" "<<#d<<":"<<d<<endl; }
// clang-format on

// DPをにぶたん
// 遅延セグ木やlower_bound使っても大丈夫かな... 尺取りの方が無難ではある
ll N, M;
ll D[1505][1505];
bool accept(ll diff) {
    vector<bool> dp(M + 1, true);
    vector<bool> ndp(M + 1);
    for (ll i = 1; i < N; i++) {
        ll right = -1;
        for (ll j = 0; j < M; j++) {
            if (!dp[j]) continue;

            ll cur = D[i - 1][j];
            while (right + 1 < M && D[i][right + 1] < cur) {
                right++;
            }
            while (right + 1 < M && D[i][right + 1] <= cur + diff) {
                ndp[right + 1] = true;
                right++;
            }
        }
        swap(dp, ndp);
        for (ll j = 0; j < M; j++) {
            ndp[j] = false;
        }
    }
    for (ll j = 0; j < M; j++) {
        if (dp[j]) return true;
    }
    return false;
}
int main() {
    ioinit();
    cin >> N >> M;
    for (ll i = 0; i < N; i++) {
        vector<ll> DD;
        for (ll j = 0; j < M; j++) {
            ll d;
            cin >> d;
            DD.push_back(d);
        }
        sort(DD.begin(), DD.end());
        for (ll j = 0; j < M; j++) {
            D[i][j] = DD[j];
        }
    }
    if (N == 1) {
        print(0);
        return 0;
    }
    const ll MX = 1000000001;
    ll left = 0;
    ll right = MX - 1;
    ll ans = MX;
    while (left <= right) {
        ll mid = (left + right) / 2;
        if (accept(mid)) {
            ans = mid;
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    if (ans == MX) ans = -1;
    print(ans);
    return 0;
}
0