結果

問題 No.2157 崖
ユーザー FplusFplusF
提出日時 2022-12-09 21:58:27
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,960 ms / 6,000 ms
コード長 1,130 bytes
コンパイル時間 2,254 ms
コンパイル使用メモリ 202,132 KB
最終ジャッジ日時 2025-02-09 08:05:49
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (long long i=0;i<(long long)(n);i++)
#define all(v) v.begin(),v.end()
using ll=long long;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
const ll INF=(1ll<<60);
template<class T> void chmin(T &a,T b){
    if(a>b){
        a=b;
    }
}
template<class T> void chmax(T &a,T b){
    if(a<b){
        a=b;
    }
}
int main(){
    ll n,m;
    cin >> n >> m;
    vector<vector<ll>> d(n,vector<ll>(m));
    rep(i,n){
        rep(j,m){
            cin >> d[i][j];
        }
        sort(all(d[i]));
    }
    vector<vector<ll>> dp(n,vector<ll>(m,INF));
    rep(i,m) dp[0][i]=0;
    rep(i,n-1){
        rep(j,m){
            if(dp[i][j]==INF) continue;
            for(ll k=m-1;0<=k;k--){
                if(d[i][j]<=d[i+1][k]){
                    chmin(dp[i+1][k],max(dp[i][j],d[i+1][k]-d[i][j]));
                }else{
                    break;
                }
            }
        }
    }
    ll ans=INF;
    rep(i,m){
        if(dp[n-1][i]==INF) continue;
        chmin(ans,dp[n-1][i]);
    }
    if(ans==INF) cout << -1 << endl;
    else cout << ans << endl;
}
0