結果
問題 | No.1095 Smallest Kadomatsu Subsequence |
ユーザー |
![]() |
提出日時 | 2020-06-26 21:33:09 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,307 ms / 2,000 ms |
コード長 | 5,369 bytes |
コンパイル時間 | 1,400 ms |
コンパイル使用メモリ | 138,408 KB |
最終ジャッジ日時 | 2025-01-11 10:55:10 |
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#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(...)#endifvoid 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 optemplate<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;}