結果
| 問題 | No.1095 Smallest Kadomatsu Subsequence |
| コンテスト | |
| ユーザー |
fastmath
|
| 提出日時 | 2020-06-26 21:27:52 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 266 ms / 2,000 ms |
| コード長 | 1,530 bytes |
| 記録 | |
| コンパイル時間 | 1,700 ms |
| コンパイル使用メモリ | 174,676 KB |
| 実行使用メモリ | 20,480 KB |
| 最終ジャッジ日時 | 2024-07-04 19:39:39 |
| 合計ジャッジ時間 | 5,254 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n;
cin >> n;
vector <int> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
const int INF = 1e18;
int ans = INF;
vector <int> l1(n,INF), l2(n,INF), r1(n,INF), r2(n,INF);
//1 - less
set <int> ms;
for (int i = 0; i < n; ++i) {
ms.insert(a[i]);
auto t = ms.find(a[i]);
if (a[i] != *ms.rbegin()) {
l2[i] = *next(t);
}
if (*ms.begin() < a[i]) {
l1[i] = *ms.begin();
}
}
ms.clear();
for (int i = n - 1; i >= 0; --i) {
ms.insert(a[i]);
auto t = ms.find(a[i]);
if (a[i] != *ms.rbegin()) {
r2[i] = *next(t);
}
if (*ms.begin() < a[i]) {
r1[i] = *ms.begin();
}
}
for (int i = 0; i < n; ++i) {
//cout << i << ' ' << l2[i] << ' ' << r2[i] << ' ' << a[i] << endl;
ans = min(ans, l1[i]+r1[i]+a[i]);
ans = min(ans, l2[i]+r2[i]+a[i]);
}
if (ans == INF)
cout << -1 << endl;
else
cout << ans << endl;
}
fastmath