結果

問題 No.2157 崖
ユーザー milanis48663220milanis48663220
提出日時 2022-12-09 21:57:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3,053 ms / 6,000 ms
コード長 3,122 bytes
コンパイル時間 1,811 ms
コンパイル使用メモリ 132,124 KB
実行使用メモリ 19,840 KB
最終ジャッジ日時 2024-04-22 22:06:01
合計ジャッジ時間 29,820 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2,329 ms
13,824 KB
testcase_14 AC 2,792 ms
18,432 KB
testcase_15 AC 2,322 ms
13,568 KB
testcase_16 AC 2,340 ms
13,952 KB
testcase_17 AC 2,369 ms
16,128 KB
testcase_18 AC 2,301 ms
15,744 KB
testcase_19 AC 2,896 ms
16,384 KB
testcase_20 AC 3,053 ms
19,712 KB
testcase_21 AC 3,037 ms
19,840 KB
testcase_22 AC 2,217 ms
15,232 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 16 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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