結果

問題 No.2157 崖
ユーザー dyktr_06dyktr_06
提出日時 2022-12-09 22:47:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,669 bytes
コンパイル時間 2,330 ms
コンパイル使用メモリ 179,200 KB
実行使用メモリ 21,652 KB
最終ジャッジ日時 2024-04-22 22:48:39
合計ジャッジ時間 11,325 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 8 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 5 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'void in(T& ...)':
main.cpp:28:55: warning: fold-expressions only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   28 | template<class... T> void in(T&... a){ (cin >> ... >> a); }
      |                                                       ^
main.cpp: In function 'void out(const T&, const Ts& ...)':
main.cpp:30:112: warning: fold-expressions only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   30 | template<class T, class... Ts> void out(const T& a, const Ts&... b){ cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; }
      |                                                                                                                ^

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; --i)
#define ALL(a) a.begin(), a.end()
#define Sort(a) sort(a.begin(), a.end())
#define RSort(a) sort(a.rbegin(), a.rend())

typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<char> vc;
typedef vector<string> vst;
typedef vector<double> vd;
typedef pair<long long, long long> P;

const long long INF = 0x1fffffffffffffff;
const long long MOD = 998244353;
const long double PI = acos(-1);
 
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; }
template<class T, class U> inline T vin(T& vec, U n) { vec.resize(n); for(int i = 0; i < (int) n; ++i) cin >> vec[i]; return vec; }
template<class T> inline void vout(T vec, string s = "\n"){ for(auto x : vec) cout << x << s; }
template<class... T> void in(T&... a){ (cin >> ... >> a); }
void out(){ cout << '\n'; }
template<class T, class... Ts> void out(const T& a, const Ts&... b){ cout << a; (cout << ... << (cout << ' ', b)); cout << '\n'; }
template<class T, class U> void inGraph(vector<vector<T>>& G, U n, U m, bool directed = false){ G.resize(n); for(int i = 0; i < m; i++){ int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); if(!directed) G[b].push_back(a); } }

template <typename S, typename T>
struct DynamicBinaryIndexedTree{
    S N;
    unordered_map<S, T> data;
    DynamicBinaryIndexedTree(S _N): N(_N + 1){
    }

    void add(S i, const T &x){
        for(++i; i < N; i += i & -i) data[i] += x;
    }

    T sum(int i){
        if(i < 0){
            return 0;
        }
        T ans = 0;
        while(i > 0){
            const auto iter = data.find(i);
            if(iter != data.end()){
                ans += iter->second;
            }
            i -= i & -i;
        }
        return ans;
    }

    T sum(int L, int R){
        return sum(R) - sum(L);
    }

    T operator[](S i){
        return sum(i + 1) - sum(i);
    }

    S lower_bound(T x){
        if(x <= 0){
            return 0;
        }
        S v = 0, r = 1;
        while(r < N) r = r << 1;
        for(S len = r; len > 0; len = len >> 1){
            if(v + len < N && data[v + len] < x){
                x -= data[v + len];
                v += len;
            }
        }
        return v;
    }
};

ll n, m;
vector<vector<ll>> d;

void input(){
    in(n, m);
    d = vector<vector<ll>>(n, vector<ll>(m));
    rep(i, n) rep(j, m) in(d[i][j]);
}
 
void solve(){
    rep(i, n) Sort(d[i]);
    ll ans = INF;

    ll ok = 1e9, ng = -1;
    while(abs(ok - ng) > 1){
        ll mid = (ok + ng) / 2;
        ll check = 1;
        DynamicBinaryIndexedTree<ll, ll> tree(1e10), tree2(1e10);
        rep(j, m){
            tree.add(d[0][j], 1);
            tree.add(d[0][j] + mid + 1, -1);
        }
        for(int i = 1; i < n; i++){
            ll cnt = 0;
            rep(j, m){
                if(tree.sum(0, d[i][j] + 1) >= 1){
                    tree2.add(d[i][j], 1);
                    tree2.add(d[i][j] + mid + 1, -1);
                    cnt++;
                }
            }
            swap(tree, tree2);
            tree2.data.clear();
            if(cnt == 0){
                check = 0;
                break;
            }
        }
        if(check) ok = mid;
        else ng = mid;
    }
    chmin(ans, ok);

    if(ans >= 1e9) out(-1);
    else out(ans);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    input();
    solve();
}
0