結果
問題 | No.1095 Smallest Kadomatsu Subsequence |
ユーザー |
![]() |
提出日時 | 2024-03-05 23:58:20 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 394 ms / 2,000 ms |
コード長 | 867 bytes |
コンパイル時間 | 2,071 ms |
コンパイル使用メモリ | 200,152 KB |
最終ジャッジ日時 | 2025-02-20 01:09:48 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;int main(){cin.tie(nullptr);ios_base::sync_with_stdio(false);int N, ans=2e9, x, y;cin >> N;vector<int> A(N+1);for(int i=1; i<=N; ++i) cin >> A[i];set<int> sl, sr;for (int i=1; i<=N; ++i) sr.insert(A[i]);for (int i=1; i<=N; i++){sr.erase(A[i]);if (sl.size() == 0 || sr.size() == 0){sl.insert(A[i]);continue;}x = *sl.begin();y = *sr.begin();if (x < A[i] && y < A[i]) ans = min(ans, x+y+A[i]);auto xx = sl.lower_bound(A[i]);auto yy = sr.lower_bound(A[i]);if (xx != sl.end() && yy != sr.end()) ans = min(ans, *xx+*yy+A[i]);sl.insert(A[i]);}if (ans == 2e9) cout << -1;else cout << ans;cout << '\n';return 0;}