結果
問題 | No.1095 Smallest Kadomatsu Subsequence |
ユーザー |
|
提出日時 | 2020-06-26 22:42:55 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 518 ms / 2,000 ms |
コード長 | 905 bytes |
コンパイル時間 | 1,884 ms |
コンパイル使用メモリ | 172,940 KB |
実行使用メモリ | 14,464 KB |
最終ジャッジ日時 | 2024-07-04 22:35:52 |
合計ジャッジ時間 | 6,857 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#include <bits/stdc++.h>using namespace std;#define int long longtemplate<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }signed main(){int n;cin >> n;int a[200010];set<int> l, r;for(int i = 0;i < n;i++){cin >> a[i];r.insert(a[i]);}int ans = LLONG_MAX;l.insert(a[0]);r.erase(a[0]);for(int i = 1;i < n-1;i++){r.erase(a[i]);{int lnum = *l.begin();int rnum = *r.begin();if(lnum < a[i] && rnum < a[i]){ans = min(ans, lnum + a[i] + rnum);}}{auto lnum = l.upper_bound(a[i]);auto rnum = r.upper_bound(a[i]);if(lnum != l.end() && rnum != r.end()){ans = min(ans, *lnum + a[i] + *rnum);}}l.insert(a[i]);}if(ans == LLONG_MAX){cout << -1 << endl;}else{cout << ans << endl;}return 0;}