結果

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/segtree>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}


template<typename T>
class Cumsum{
    public:
    Cumsum(vector<T> v): v(v){
        n = v.size();
        v_cumsum = vector<T>(n+1, T(0));
        for(int i = 0; i < n; i++) v_cumsum[i+1] = v_cumsum[i]+v[i];
    }
    /**
     * v[l] + ... + v[r-1]
     */ 
    T sum(int l, int r){
        if(r <= l) return T(0);
        if(r > n) r = n;
        if(l < 0) l = 0;
        return v_cumsum[r]-v_cumsum[l];
    }
    private:
    int n;
    vector<T> v;
    vector<T> v_cumsum;
};

const ll inf = 1e18;

ll op(ll x, ll y){
    return min(x, y);
}

ll e(){
    return inf;
}

using Seg = atcoder::segtree<ll, op, e>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m; cin >> n >> m;
    auto d = vec2d(n, m, 0ll);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> d[i][j];
        }
        sort(d[i].begin(), d[i].end());
    }
    auto judge = [&](ll x){
        auto ok = vec2d(n, m, false);
        for(int j = 0; j < m; j++) ok[0][j] = true;
        for(int i = 1; i < n; i++){
            int l = 0, r = 0;
            vector<int> v(m);
            for(int j = 0; j < m; j++) {
                if(ok[i-1][j]) v[j] = 1;
            }
            auto cumsum = Cumsum<int>(v);
            for(int j = 0; j < m; j++){
                while(l < m && d[i-1][l] < d[i][j]-x) l++;
                while(r < m && d[i-1][r] <= d[i][j]) r++;
                if(cumsum.sum(l, r) > 0) ok[i][j] = true;
            }
        }
        for(int j = 0; j < m; j++){
            if(ok[n-1][j]) return true;
        }
        return false;
    };
    if(judge(0)){
        cout << 0 << endl;
        return 0;
    }
    ll l = 0, r = inf;
    while(r-l > 1){
        ll x = (l+r)/2;
        if(judge(x)) r = x;
        else l = x;
    }
    if(r == inf){
        cout << -1 << endl;
    }else{
        cout << r << endl;
    }
}
0