結果
問題 | No.1095 Smallest Kadomatsu Subsequence |
ユーザー |
|
提出日時 | 2020-08-19 20:49:26 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 261 ms / 2,000 ms |
コード長 | 1,366 bytes |
コンパイル時間 | 1,601 ms |
コンパイル使用メモリ | 176,004 KB |
実行使用メモリ | 29,824 KB |
最終ジャッジ日時 | 2024-10-12 05:15:41 |
合計ジャッジ時間 | 5,455 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#include <algorithm> #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() #define ll long long #define ld long double #define INF 1000000000000000000 typedef pair<ll, ll> pll; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<ll> A(N); rep(i, N) { cin >> A[i]; } vector<ll> lmin(N), rmin(N); ll lmi = INF, rmi = INF; rep(i, N) { lmi = min(lmi, A[i]); lmin[i] = lmi; } for (int i = N - 1; i > -1; i--) { rmi = min(rmi, A[i]); rmin[i] = rmi; } vector<ll> lse(N), rse(N); set<ll> sel, ser; sel.insert(INF), ser.insert(INF); rep(i, N) { sel.insert(A[i]); if (i - 1 >= 0) lse[i] = *sel.upper_bound(A[i]); } for (int i = N - 1; i > -1; i--) { ser.insert(A[i]); if (i + 1 <= N - 1) rse[i] = *ser.upper_bound(A[i]); } ll atype = INF, btype = INF; for (int i = 1; i < N - 1; i++) { if (A[i] > rmin[i + 1] && A[i] > lmin[i - 1]) atype = min(atype, A[i] + rmin[i + 1] + lmin[i - 1]); } for (int i = 1; i < N - 1; i++) { btype = min(btype, A[i] + lse[i] + rse[i]); } ll ans = min(atype, btype); cout << (ans == INF ? -1 : ans) << endl; }