結果
問題 | No.2157 崖 |
ユーザー |
|
提出日時 | 2022-12-09 22:52:45 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 645 ms / 6,000 ms |
コード長 | 2,663 bytes |
コンパイル時間 | 2,311 ms |
コンパイル使用メモリ | 203,868 KB |
最終ジャッジ日時 | 2025-02-09 08:40:53 |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 22 |
ソースコード
#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; }