結果
問題 | No.1095 Smallest Kadomatsu Subsequence |
ユーザー |
![]() |
提出日時 | 2020-06-28 17:12:07 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 466 ms / 2,000 ms |
コード長 | 2,708 bytes |
コンパイル時間 | 3,036 ms |
コンパイル使用メモリ | 200,156 KB |
最終ジャッジ日時 | 2025-01-11 13:24:53 |
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#include <bits/stdc++.h>using namespace std;//#define int long long#define all(v) (v).begin(), (v).end()#define rall(v) (v).rbegin(), (v).rend()#define rep(i,n) for(int i=0;i<n;++i)#define rep1(i,n) for(int i=1;i<n;++i)#define exrep(i, a, b) for(ll i = a; i < b; i++)#define out(x) cout << x << endl#define EPS (1e-7)#define gearup ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);typedef long long ll;typedef long double ld;typedef unsigned long long ull;typedef vector<int> vi;typedef vector<char> vc;typedef vector<bool> vb;typedef vector<double> vd;typedef vector<string> vs;typedef vector<pair<int,int> > vpii;typedef vector<vector<int> > vvi;typedef vector<vector<char> > vvc;typedef vector<vector<bool> > vvb;typedef vector<vector<double> > vvd;typedef vector<vector<string> > vvs;typedef vector<ll> vl;typedef vector<vector<ll> > vvl;typedef vector<vector<vector<ll> > > vvvl;ll MOD = 1000000007;const long long L_INF = 1LL << 60;const int INF = 2147483647; // 2^31-1const double PI = acos(-1);//cout<<fixed<<setprecision(10);template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}template<class T> void debug(T v){rep(i,v.size()) cout<<v[i]<<" ";cout<<endl;}const ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};const ll dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};//以降 cin の入力元が 'input.txt' になる//std::ifstream in("input.txt");//std::cin.rdbuf(in.rdbuf());signed main(){gearup;ll n; cin >> n;vl a(n);rep(i,n)cin>>a[i];vl sl_mi(n+1,L_INF),sr_mi(n+1,L_INF);rep(i,n)sl_mi[i+1] = min(sl_mi[i],a[i]);for(int i=n-1;i>=0;i--)sr_mi[i] = min(sr_mi[i+1],a[i]);ll res = L_INF;//Bを固定する Bが大きい場合for(int i=1;i<n-1;i++){ll B = a[i];ll A = sl_mi[i];ll C = sr_mi[i+1];if(A < B && B > C)res = min(res,A+B+C);}//Bが小さい場合vl dp_l(n);set<ll> l;rep(i,n){auto itr = l.upper_bound(a[i]);if(itr == l.end())dp_l[i] = L_INF;else dp_l[i] = *itr;l.insert(a[i]);}vl dp_r(n);set<ll> r;for(int i=n-1;i>=0;i--){auto itr = r.upper_bound(a[i]);if(itr == r.end())dp_r[i] = L_INF;else dp_r[i] = *itr;r.insert(a[i]);}ll res1 = L_INF;//Bを固定する Bが小さい場合for(int i=1;i<n-1;i++){ll B = a[i];ll A = dp_l[i];ll C = dp_r[i];if(A > B && B < C)res1 = min(res1,A+B+C);}res = min(res,res1);if(res == L_INF)out(-1);else out(res);}