結果

問題 No.2157 崖
ユーザー FplusFplusF
提出日時 2022-12-09 21:55:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 5,912 ms / 6,000 ms
コード長 1,041 bytes
コンパイル時間 2,081 ms
コンパイル使用メモリ 199,720 KB
最終ジャッジ日時 2025-02-09 07:58:24
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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];
        }
    }
    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;
            rep(k,m){
                if(d[i][j]<=d[i+1][k]){
                    chmin(dp[i+1][k],max(dp[i][j],d[i+1][k]-d[i][j]));
                }
            }
        }
    }
    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