結果

問題 No.2157 崖
ユーザー eQeeQe
提出日時 2023-08-13 20:30:10
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,351 ms / 6,000 ms
コード長 9,202 bytes
コンパイル時間 5,916 ms
コンパイル使用メモリ 329,664 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2024-05-01 11:30:13
合計ジャッジ時間 26,886 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 1,864 ms
8,832 KB
testcase_14 AC 2,026 ms
10,880 KB
testcase_15 AC 1,967 ms
8,704 KB
testcase_16 AC 1,952 ms
8,704 KB
testcase_17 AC 1,733 ms
9,856 KB
testcase_18 AC 1,728 ms
9,728 KB
testcase_19 AC 2,351 ms
10,112 KB
testcase_20 AC 2,213 ms
11,648 KB
testcase_21 AC 2,217 ms
11,520 KB
testcase_22 AC 1,622 ms
9,472 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 4 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define RD(T,...) T __VA_ARGS__;li(__VA_ARGS__)
#define LL(...) RD(ll,__VA_ARGS__)
#define JO(a,b) a##b
#define jo(a,b) JO(a,b)
#define uq_symbol(a) jo(a,__LINE__)
#define FE(v,e,...) for(au&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):v)
#define FO(n) for(ll uq_symbol(i)=n;uq_symbol(i)-->0;)
#define FOR(i,...) for(au[i,i##O,i##E]=rng(0,__VA_ARGS__);i<i##O;i+=i##E)
#define fe(v,...) FE(v,__VA_ARGS__)
#define fo(i,...) FO##__VA_OPT__(R)(i __VA_OPT__(,__VA_ARGS__))
#define of(i,...) for(au[i,i##O,i##E]=rng(1,__VA_ARGS__);i>=i##O;i-=i##E)
#define I template
#define J class
#define au auto
#define rr return
using std::cin,std::cout,std::begin,std::end,std::rbegin;
using std::swap,std::move,std::abs,std::prev,std::next;
using std::tuple,std::array,std::bitset,std::minmax,std::get;
using vo=void;using bo=bool;
vo solve();
I<J A,J B>constexpr bo same=std::is_same_v<A,B>;
using is=std::istream;using os=std::ostream;
using dd=long double;using ll=long long;using ull=unsigned long long;
using lll=__int128_t;using ulll=__uint128_t;using str=std::string;
using namespace atcoder;using ml=modint;au&operator<<(os&o,const ml&x){rr o<<x.val();}
constexpr ll oo=3e18;
constexpr ll dx[]{-1,0,1,0,-1,1,1,-1},dy[]{0,-1,0,1,-1,-1,1,1};
constexpr char sp=32;
constexpr char nl=10;
au rng(bo s,ll a,ll b=oo,ll c=1){if(b==oo)b=a,(s?b:a)=0;rr tuple{a-s,b,c};}

I<J T>T zz(T x){rr x<0?-x:x;}
I<J T>ll len(const T&a){rr a.size();}
I<J...A>au max(const A&...a){rr max(std::initializer_list<std::common_type_t<A...>>{a...});}

struct edg{
  ll t,w;
  edg(){}
  edg(ll t,ll w=1):t(t),w(w){}
  friend os&operator<<(os&o,const edg&e){rr o<<e.t<<sp<<e.w;}
};

I<J A,J B=A>struct cp{
  A a={};B b={};
  cp(){}
  cp(A a,B b):a(a),b(b){}
  cp(std::pair<A,B>p):a(p.first),b(p.second){}
  bo operator==(const cp&c)const{rr a==c.a&&b==c.b;}
  bo operator<(const cp&c)const{rr a!=c.a?a<c.a:b<c.b;}
  bo operator>(const cp&c)const{rr a!=c.a?a>c.a:b>c.b;}

  friend is&operator>>(is&i,cp&c){rr i>>c.a>>c.b;}
  friend os&operator<<(os&o,const cp&c){rr o<<c.a<<sp<<c.b;}
};using cl=cp<ll>;
I<J A,J B=A,J C=A>struct tr{
  A a={};B b={};C c={};
  tr(){}
  tr(A a,B b,C c):a(a),b(b),c(c){}
  bo operator==(const tr&t)const{rr a==t.a&&b==t.b&&c==t.c;}

  au operator<(const tr&t)const{rr a!=t.a?a<t.a:b!=t.b?b<t.b:c<t.c;}
  friend is&operator>>(is&i,tr&t){rr i>>t.a>>t.b>>t.c;}
  friend os&operator<<(os&o,const tr&t){rr o<<t.a<<sp<<t.b<<sp<<t.c;}
};using tl=tr<ll>;

I<J T>T&srt(T&a){sort(all(a));rr a;}
I<J T>T&rv(T&a){reverse(all(a));rr a;}
I<J T>decltype(au)bg(T&a){rr*begin(a);}
I<J T>decltype(au)rb(T&a){rr*rbegin(a);}
I<J T>au pof(T&a){au r=bg(a);a.pop_front();rr r;}
I<J T>au pob(T&a){au r=rb(a);a.pop_back();rr r;}
I<J T>au pot(T&a){au r=a.top();a.pop();rr r;}

I<J T,J U=T>using map=std::map<T,U>;
I<J T,J U>os&operator<<(os&o,const map<T,U>&m){fe(m,e)o<<e.first<<sp<<e.second<<nl;rr o;}

I<J T,J U=T>using umap=std::unordered_map<T,U>;
I<J T,J U>os&operator<<(os&o,const umap<T,U>&m){fe(m,e)o<<e.first<<sp<<e.second<<nl;rr o;}

I<J T>struct set:std::set<T>{
  set(const std::vector<T>&a={}){fe(a,e)this->emplace(e);}
  friend os&operator<<(os&o,const set&s){fe(s,e)o<<e<<str(&e!=&s.back(),sp);rr o;}
};
I<J T>struct mset:std::multiset<T>{
  mset(const std::vector<T>&a={}){fe(a,e)this->emplace(e);}
  friend os&operator<<(os&o,const mset&s){fe(s,e)o<<e<<str(&e!=&s.back(),sp);rr o;}
};

I<J T>T pof(set<T>&s){T r=bg(s);s.erase(s.begin());rr r;}
I<J T>T pob(set<T>&s){T r=rb(s);s.erase(prev(s.end()));rr r;}
I<J T>T pof(mset<T>&s){T r=bg(s);s.erase(s.begin());rr r;}
I<J T>T pob(mset<T>&s){T r=rb(s);s.erase(prev(s.end()));rr r;}

I<J V>struct ve;
I<J V>constexpr bo isv=0;
I<J V>constexpr bo isv<ve<V>> =1;
I<J V>constexpr bo isv<std::vector<V>> =1;
I<J V>au rawv(V){if constexpr(isv<V>)rr rawv(V(1)[0]);else rr V();}

I<J V>struct ve:std::vector<V>{
  using std::vector<V>::vector;
  using T=decltype(rawv(V()));
  I<J U>ve(const std::vector<U>&v={}){fe(v,e)this->eb(e);}

  ve&operator+=(const ve&u){au&v=*this;fo(i,len(v))v[i]+=u[i];rr v;}
  ve&operator-=(const ve&u){au&v=*this;fo(i,len(v))v[i]-=u[i];rr v;}
  ve&operator^=(const ve&u){fe(u,e)this->eb(e);rr*this;}
  ve&operator+=(const T&x){au&v=*this;fe(v,e)e+=x;rr v;}
  ve&operator-=(const T&x){au&v=*this;fe(v,e)e-=x;rr v;}
  ve&operator*=(const T&x){au&v=*this;fe(v,e)e*=x;rr v;}
  I<size_t n>au&operator+=(const bitset<n>&a){fo(i,n)(*this)[i]+=a[i];rr*this;}
  I<size_t n>au&operator-=(const bitset<n>&a){fo(i,n)(*this)[i]-=a[i];rr*this;}

  ve operator+(const ve&u)const{rr ve(*this)+=u;}
  ve operator-(const ve&u)const{rr ve(*this)-=u;}
  ve operator^(const ve&u)const{rr ve(*this)^=u;}
  ve operator+(const T&x)const{rr ve(*this)+=x;}
  ve operator-(const T&x)const{rr ve(*this)-=x;}
  ve operator*(const T&x)const{rr ve(*this)*=x;}
  ve&operator++(){rr*this+=1;}
  ve&operator--(){rr*this-=1;}
  ve operator-()const{rr ve(*this)*=-1;}

  au lower_bound(const V&x)const{rr std::lower_bound(all(*this),x);}
  au upper_bound(const V&x)const{rr std::upper_bound(all(*this),x);}

  I<J F>au scan(F f)const{cp<T,bo>r;fe(*this,e)if constexpr(!isv<V>)r.b?f(r.a,e),r:r={e,1};else if(au s=e.scan(f);s.b)r.b?f(r.a,s.a),r:r=s;rr r;}
  T max()const{rr scan([](T&a,const T&b){a<b?a=b:0;}).a;}

  vo fill(T x={}){fe(*this,e)if constexpr(isv<V>)e.fill(x);else e=x;}

};
I<J T=ll,size_t n,size_t i=0>au vec(const ll(&s)[n],T x={}){
  if constexpr(n==i+1)rr ve<T>(s[i],x);
  else{au X=vec<T,n,i+1>(s,x);rr ve<decltype(X)>(s[i],X);}
}
I<ll n,J...A>vo setsize(const ll(&l)[n],A&...a){((a=vec(l,rawv(a))),...);}
I<J T>using vve=ve<ve<T>>;using vl=ve<ll>;using vvl=vve<ll>;
using gl=ve<ve<ll>>;using ge=ve<ve<edg>>;

struct io{io(){cin.tie(0)->sync_with_stdio(0);
  cout<<std::fixed<<std::setprecision(15);std::cerr<<nl;}}io;
os&operator<<(os&o,const ulll&x){rr(x<10?o:o<<x/10)<<ll(x%10);}
os&operator<<(os&o,const lll&x){rr o<<str(x<0,'-')<<ulll(zz(x));}
I<size_t n>os&operator<<(os&o,const bitset<n>&b){fo(i,n)o<<b[i];rr o;}
I<J...A>os&operator<<(os&o,const tuple<A...>&t){
  apply([&](const au&...a){ll i=0;(((o<<a<<str(++i!=sizeof...(a),sp))),...);},t);rr o;}
I<J V>os&operator<<(os&o,const std::vector<V>&v){fe(v,e)o<<e<<str(&e!=&rb(v),isv<V>?nl:sp);rr o;}
I<char c=sp,J...A>vo pp(const A&...a){ll i=0;((cout<<a<<str(++i!=sizeof...(a),c)),...);cout<<nl;}
I<J V>is&operator>>(is&i,std::vector<V>&v){fe(v,e)i>>e;rr i;}
I<J...A>vo li(A&...a){(cin>>...>>a);}

I<J F>ll bsl(F f,ll o,ll n){while(zz(o-n)>1)(f((o+n)/2)?o:n)=(o+n)/2;rr o;}
I<J F>struct rec:F{rec(F&&f):F(std::forward<F>(f)){}I<J...A>decltype(au)operator()(A&&...a)const{rr F::operator()(*this,std::forward<A>(a)...);}};

template<class S,S op(S,S),S e()>struct swag{
  ve<S>pre,pre_row,suf,suf_row;

  swag(const ve<S>&a={}){
    fe(a,x)eb(x);
  }

  ll size()const{
    return len(pre)+len(suf);
  }

  template<class...A>vo eb(A&&...a){
    S x(std::forward<A>(a)...);
    suf.eb(op((len(suf)?rb(suf):e()),x));
    suf_row.eb(x);
  }

  S pof(){
    move();
    pob(pre_row);
    return pob(pre);
  }

  S front(){
    move();
    return rb(pre_row);
  }

  S fold(){
    if(!size())return e();
    if(!len(pre))return rb(suf);
    if(!len(suf))return rb(pre);
    return op(rb(pre),rb(suf));
  }

  vo move(){
    if(!len(pre)){
      while(len(suf)){
        pre.eb(op(rb(suf_row),(len(pre)?rb(pre):e())));
        pre_row.eb(rb(suf_row));
        pob(suf);
        pob(suf_row);
      }
    }
  }

  friend os&operator<<(os&o,const swag&s){auto u=s.pre_row,v=s.suf_row;return o<<rv(u)<<": "<<v;}
};

using std::true_type,std::false_type;
template<class T>struct is_tp:false_type{};
template<class...A>struct is_tp<tuple<A...>>:true_type{};

template<class T>struct is_ar:false_type{};
template<class T,ll n>struct is_ar<std::array<T,n>>:true_type{};

template<class T>struct is_cp:false_type{};
template<class A,class B>struct is_cp<cp<A,B>>:true_type{};
template<class T>constexpr bo is_cp_v = is_cp<T>::value;

template<class T>struct is_tr:false_type{};
template<class A,class B,class C>struct is_tr<tr<A,B,C>>:true_type{};
template<class T>constexpr bo is_tr_v = is_tr<T>::value;

template<class T,auto x>T fill(){
  if constexpr(same<T,decltype(x)>)return x;
  else if constexpr(is_cp_v<T>)return{x,x};
  else if constexpr(is_tr_v<T>)return{x,x,x};
  else if constexpr(same<T,array<ll,std::tuple_size_v<T>>>){T r;fe(r,e)e=x;return r;}
  assert(0);
}

template<class T>T omax(T a,T b){return b<a?a:b;}
template<class T>T omin(T a,T b){return a<b?a:b;}
template<class T>T osum(T a,T b){return a+b;}

int main(){ll T=1;fo(T)solve();}
vo solve(){
  LL(N,M);
  vve<int>a;setsize({N,M},a);li(a);
  fe(a,e)srt(e);

  ll ans=bsl([&](ll X){//崖をX以下にできるか
    ve<int>dp(M,1),nx(M);
    fo(i,1,N){
      using S=cp<int>;
      swag<S,omax<S>,fill<S,0>>sw;
      ll cu=0;
      fo(j,M){
        while(len(sw)&&a[i][j]-X>sw.front().b)sw.pof();
        while(cu<M&&a[i-1][cu]<a[i][j]-X)cu++;
        while(cu<M&&a[i-1][cu]<=a[i][j])sw.eb(dp[cu],a[i-1][cu]),cu++;
        nx[j]=sw.fold().a;
      }
      swap(dp,nx);
      nx.fill();
    }
    rr dp.max()==1;
  },1e9,-1);
  pp(ans!=1e9?ans:-1);
}
0