結果
問題 | No.1095 Smallest Kadomatsu Subsequence |
ユーザー |
|
提出日時 | 2020-07-15 12:17:26 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 249 ms / 2,000 ms |
コード長 | 1,993 bytes |
コンパイル時間 | 10,880 ms |
コンパイル使用メモリ | 280,188 KB |
最終ジャッジ日時 | 2025-01-11 21:05:04 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#pragma region #pragma GCC target("avx2") #pragma GCC optimize("03") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; typedef unsigned long long ull; #define endl "\n" #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define PII pair<int, int> #define PLL pair<ll, ll> #define VPII vector<PII> #define VPLL vector<PLL> #define ALL(x) (x).begin(), (x).end() constexpr int INF=1<<30; constexpr ll LINF=1LL<<60; constexpr ll mod=1e9+7; constexpr int NIL = -1; template<class T>inline bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; } template<class T>inline bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; } #pragma endregion //------------------- const int MX = 2e5+4; int n; ll a[MX]; ll solve() { vector<ll> mn(MX); vector<ll> rmn(MX); ll cur = LINF; FOR(i,0,n-1) { chmin(cur, a[i]); mn[i] = cur; } cur = LINF; FOR(i,0,n-1) { chmin(cur, a[n-1-i]); rmn[n-1-i] = cur; } ll ans = LINF; FOR(i,1,n-2) { if(mn[i-1] < a[i] and a[i] > rmn[i+1]) { chmin(ans, a[i] + mn[i-1] + rmn[i+1]); } } set<ll> st; st.insert(a[0]); st.insert(LINF); FOR(i,1,n-1) { mn[i] = *st.upper_bound(a[i]); st.insert(a[i]); } set<ll> rst; rst.insert(a[n-1]); rst.insert(LINF); for(int i=n-2; i>=0; i--) { rmn[i] = *rst.upper_bound(a[i]); rst.insert(a[i]); } FOR(i,1,n-2) { if(mn[i] > a[i] and a[i] < rmn[i]) { chmin(ans, mn[i] + a[i] + rmn[i]); } } return ans; } int main(){ cin.tie(0); ios::sync_with_stdio(false); //cout << fixed << setprecision(15); cin >> n; FOR(i,0,n-1) cin >> a[i]; ll ans = solve(); if(ans == LINF) { cout << -1 << endl; return 0; } else { cout << ans << endl; return 0; } return 0; }