結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー Imperi_NightImperi_Night
提出日時 2020-06-26 21:33:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,320 ms / 2,000 ms
コード長 5,369 bytes
コンパイル時間 1,475 ms
コンパイル使用メモリ 144,164 KB
実行使用メモリ 201,984 KB
最終ジャッジ日時 2024-07-04 19:50:38
合計ジャッジ時間 12,473 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 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 3 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 27 ms
8,448 KB
testcase_14 AC 28 ms
8,192 KB
testcase_15 AC 27 ms
8,192 KB
testcase_16 AC 26 ms
8,192 KB
testcase_17 AC 26 ms
8,320 KB
testcase_18 AC 26 ms
8,192 KB
testcase_19 AC 26 ms
8,320 KB
testcase_20 AC 27 ms
8,192 KB
testcase_21 AC 25 ms
8,320 KB
testcase_22 AC 25 ms
8,192 KB
testcase_23 AC 1,299 ms
201,728 KB
testcase_24 AC 1,309 ms
201,984 KB
testcase_25 AC 1,304 ms
201,984 KB
testcase_26 AC 1,320 ms
201,856 KB
testcase_27 AC 1,313 ms
201,856 KB
testcase_28 AC 371 ms
107,264 KB
testcase_29 AC 384 ms
107,264 KB
testcase_30 AC 772 ms
154,624 KB
testcase_31 AC 774 ms
154,752 KB
testcase_32 AC 777 ms
154,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "main.cpp"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <chrono>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#include <random>
#include <utility>
#include <limits>
#include <list>
#include <type_traits>

/* template start */
 
#define rep(i, a, b) for (long long i = (a); (i) < (b); (i)++)
#define all(i) i.begin(), i.end()

#ifdef LOCAL
#define debug(...) std::cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out(){std::cerr<<std::endl;}

template<typename Head,typename... Tail>
void debug_out(Head h,Tail... t){
  std::cerr<<" "<<h;
  if(sizeof...(t)>0)std::cout<<" :";
  debug_out(t...);
}
 
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
  for (std::size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
 
template<typename T1,typename T2>
inline bool chmax(T1& a,T2 b){return a<b && (a=b,true);}
 
template<typename T1,typename T2>
inline bool chmin(T1& a,T2 b){return a>b && (a=b,true);}

template<typename Num>
constexpr Num mypow(Num a, long long b) {
  if(b==0)return 1;
  if (a==0)return 0;
  Num x = 1;
  while (b > 0) {
    if(b & 1)x*=a;
    a*=a;
    b >>= 1;
  }
  return x;
}

/* template end */

using ll = long long;

#line 2 "/mnt/c/Users/Imperi/Documents/cp-library/lib/SegmentTree/DynamicSegmentTree.hpp"

#line 5 "/mnt/c/Users/Imperi/Documents/cp-library/lib/SegmentTree/DynamicSegmentTree.hpp"

//Monoid
// type value_t
// static var id
// static (value_t,value_t)->value_t op

template<typename Monoid>
class DynamicSegmentTree{
  public:
  using value_t=typename Monoid::value_t;
  using size_t=std::size_t;
  private:

  struct Node{
    value_t val;
    Node *left,*right,*par;
    Node(Node *par_=nullptr):val(Monoid::id),left(),right(),par(par_){}
    ~Node(){
      if(left)delete left;
      if(right)delete right;
      left=nullptr;right=nullptr;
      par=nullptr;
    }
  };

  using node_ptr=Node *;
  
  size_t n,n0;
  node_ptr root;

  value_t fold(size_t a,size_t b,const node_ptr& now,size_t l,size_t r){
    if(a<=l&&r<=b)return now->val;
    if(b<=l||r<=a)return Monoid::id;
    value_t lval=(now->left)?fold(a,b,now->left,l,l+(r-l)/2):Monoid::id;
    value_t rval=(now->right)?fold(a,b,now->right,l+(r-l)/2,r):Monoid::id;
    return Monoid::op(lval,rval);
  }

  void copy_dfs(node_ptr& node,const node_ptr& from){
    node->val=from->val;
    if(from->left){
      node->left=new Node(node);
      copy_dfs(node->left,from->left);
    }
    if(from->right){
      node->right=new Node(node);
      copy_dfs(node->right,from->right);
    }
  }

  public:
  DynamicSegmentTree(size_t n_=0):n(n_),root(nullptr){
    n0=1;
    while(n0<n)n0<<=1;
  }
  DynamicSegmentTree(const DynamicSegmentTree& from){
    n=from.n;n0=from.n0;root=nullptr;
    if(from.root){
      root=new Node();
      copy_dfs(root,from.root);
    }
  }
  DynamicSegmentTree& operator=(const DynamicSegmentTree& from){
    n=from.n;n0=from.n0;root=nullptr;
    if(from.root){
      root=new Node();
      copy_dfs(root,from.root);
    }
  }
  DynamicSegmentTree(DynamicSegmentTree&&)=default;
  DynamicSegmentTree& operator=(DynamicSegmentTree&&)=default;
  ~DynamicSegmentTree(){
    if(root)delete root;
    root=nullptr;
  }

  void update(size_t i,value_t val,bool change){
    assert(0<=i&&i<n);
    if(!root)root=new Node();
    node_ptr now=root;
    size_t l=0,r=n0;
    while(r-l>1){
      size_t mid=l+(r-l)/2;
      if(i<mid){
        if(!now->left)now->left=new Node(now);
        now=now->left;
        r=mid;
      }else{
        if(!now->right)now->right=new Node(now);
        now=now->right;
        l=mid;
      }
    }
    if(change)now->val=val;
    else now->val=Monoid::op(now->val,val);

    while(now->par){
      now=now->par;
      now->val=Monoid::op((now->left)?now->left->val:Monoid::id,
                          (now->right)?now->right->val:Monoid::id);
    }
  }

  value_t fold(size_t a,size_t b){
    assert(0<=a&&a<=b&&b<=n);
    if(!root)return Monoid::id;
    return fold(a,b,root,0,n0);
  }
};
#line 85 "main.cpp"

constexpr ll INF=1e9;

struct Min{
  using value_t=ll;
  static constexpr value_t id=INF;
  static constexpr value_t op(value_t a,value_t b){
    return std::min(a,b);
  }
};

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  ll n;
  std::cin>>n;

  DynamicSegmentTree<Min> up(INF),down(INF);

  ll ans=INF;

  std::set<ll> set;

  rep(i,0,n){
    ll a;
    std::cin>>a;

    if(up.fold(a+1,INF)!=INF)chmin(ans,up.fold(a+1,INF)+a);
    if(down.fold(0,a)!=INF)chmin(ans,down.fold(0,a)+a);

    if(auto itr=set.upper_bound(a);itr!=set.end())down.update(a,a+*itr,true);
    if((!set.empty())&& (*set.begin()) < a)up.update(a,*set.begin()+a,true);

    set.insert(a);
  }

  if(ans!=INF)std::cout<<ans<<"\n";
  else std::cout<<-1<<"\n";

  return 0;
}
0