結果
| 問題 |
No.1095 Smallest Kadomatsu Subsequence
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-05-31 15:09:08 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,030 bytes |
| コンパイル時間 | 1,781 ms |
| コンパイル使用メモリ | 174,568 KB |
| 実行使用メモリ | 17,152 KB |
| 最終ジャッジ日時 | 2024-11-08 21:16:52 |
| 合計ジャッジ時間 | 30,894 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 10 TLE * 11 -- * 9 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>
const ll INF = 1e18;
int main() {
int n;
cin >> n;
vector<ll> a(n);
rep(i,n) cin >> a[i];
vector<ll> mxl(n), mxr(n), mnl(n, INF), mnr(n, INF);
set<ll> sl, sr;
for (int i = 1; i < n; i++) {
mnl[i] = min(mnl[i-1], a[i-1]);
sl.insert(a[i-1]);
auto it = upper_bound(sl.begin(), sl.end(), a[i]);
if (it == sl.end()) mxl[i] = INF;
else mxl[i] = *it;
}
for (int i = n-2; i >= 0; i--) {
mnr[i] = min(mnr[i+1], a[i+1]);
sr.insert(a[i+1]);
auto it = upper_bound(sr.begin(), sr.end(), a[i]);
if (it == sr.end()) mxr[i] = INF;
else mxr[i] = *it;
}
ll ans = INF;
for (int i = 1; i < n-1; i++) {
if (a[i] > mnl[i] && a[i] > mnr[i]) {
ans = min(ans, a[i]+mnl[i]+mnr[i]);
}
if (a[i] < mxl[i] && a[i] < mxr[i]) {
ans = min(ans, a[i]+mxl[i]+mxr[i]);
}
}
if (ans == INF) ans = -1;
cout << ans << endl;
}