結果

問題 No.2157 崖
ユーザー t98slider
提出日時 2022-12-09 21:44:41
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 809 ms / 6,000 ms
コード長 1,256 bytes
コンパイル時間 1,789 ms
コンパイル使用メモリ 174,292 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-10-14 21:23:57
合計ジャッジ時間 9,039 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w;
    cin >> h >> w;
    vector<vector<int>> A(h, vector<int>(w));
    for(int y = 0; y < h; y++){
        for(int x = 0; x < w; x++){
            cin >> A[y][x];
        }
        sort(A[y].begin(), A[y].end());
    }
    auto f = [&](int lim){
        vector<int> dp(w + 1);
        dp[0] = 1;
        for(int y = 0; y + 1 < h; y++){
            vector<int> ndp(w + 1);
            for(int x = 0, l = 0, r = 0; x < w; x++){
                dp[x + 1] += dp[x];
                if(dp[x] == 0)continue;
                while(l < w && A[y + 1][l] < A[y][x])l++;
                while(r < w && A[y + 1][r] <= A[y][x] + lim)r++;
                ndp[l]++;
                ndp[r]--;
            }
            swap(dp, ndp);
        }
        for(int x = 0; x < w; x++){
            if(dp[x] >= 1)return true;
            dp[x + 1] += dp[x];
        }
        return false;
    };
    int ng = -1, ok = 1 << 30, mid;
    if(!f(ok)){
        cout << -1 << '\n';
        return 0;
    }
    while(ng + 1 < ok){
        mid = (ok + ng) / 2;
        if(f(mid)) ok = mid;
        else ng = mid;
    }
    cout << ok << '\n';
}
0