結果
| 問題 | 
                            No.2157 崖
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2022-12-09 23:49:50 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,405 bytes | 
| コンパイル時間 | 2,201 ms | 
| コンパイル使用メモリ | 201,048 KB | 
| 最終ジャッジ日時 | 2025-02-09 08:56:34 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 18 TLE * 4 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
typedef long long ll;
int main() {
    cin.tie(0); ios::sync_with_stdio(false);
    
    int N, M; cin >> N >> M;
    vector<int> D[N];
    rep(i, N) rep(j, M) {
        int d; cin >> d;
        D[i].pb(d);
    }
    rep(i, N) sort(D[i].begin(), D[i].end());
    
    int right[N][M];
    for (int i=1; i<N; i++) rep(j, M) {
        right[i][j] = upper_bound(D[i-1].begin(), D[i-1].end(), D[i][j])-D[i-1].begin();
    };
    int l = 0;
    int r = 1000000010;
    
    while (l<=r) {
        int m = (l+r)/2;
        
        bool dp[M];
        rep(i, M) dp[i] = true;
        
        for (int i=1; i<N; i++) {
            vector<int> acc = {0};
            rep(j, M) acc.pb(acc.back()+(dp[j] ? 1 : 0));
            
            bool ndp[M];
            rep(j, M) ndp[j] = false;
            
            rep(j, M) {
                int le = 0;
                while (le<M && D[i][j]-D[i-1][le]>m) le++;
                int ri = right[i][j];
                if (le<=ri && acc[ri]-acc[le]>0) ndp[j] = true;
            }
            rep(j, M) dp[j] = ndp[j];
        }
        
        bool ok = false;
        rep(i, M) if (dp[i]) ok = true;
        if (ok) r = m-1;
        else l = m+1;
    }
    
    int ans = l;
    if (ans>1000000010) cout << -1 << endl;
    else cout << ans << endl;
}