結果

問題 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,258 ms / 2,000 ms
コード長 5,369 bytes
コンパイル時間 1,525 ms
コンパイル使用メモリ 142,680 KB
実行使用メモリ 202,004 KB
最終ジャッジ日時 2023-09-18 03:15:49
合計ジャッジ時間 12,531 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 28 ms
8,464 KB
testcase_14 AC 28 ms
8,184 KB
testcase_15 AC 28 ms
8,248 KB
testcase_16 AC 28 ms
8,204 KB
testcase_17 AC 29 ms
8,452 KB
testcase_18 AC 29 ms
8,352 KB
testcase_19 AC 28 ms
8,208 KB
testcase_20 AC 28 ms
8,196 KB
testcase_21 AC 28 ms
8,344 KB
testcase_22 AC 28 ms
8,188 KB
testcase_23 AC 1,233 ms
201,740 KB
testcase_24 AC 1,249 ms
202,004 KB
testcase_25 AC 1,252 ms
201,832 KB
testcase_26 AC 1,258 ms
201,788 KB
testcase_27 AC 1,244 ms
201,864 KB
testcase_28 AC 362 ms
107,396 KB
testcase_29 AC 392 ms
107,284 KB
testcase_30 AC 766 ms
154,576 KB
testcase_31 AC 782 ms
154,776 KB
testcase_32 AC 776 ms
154,624 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