結果
問題 | No.1095 Smallest Kadomatsu Subsequence |
ユーザー |
|
提出日時 | 2020-06-26 22:27:41 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 80 ms / 2,000 ms |
コード長 | 1,549 bytes |
コンパイル時間 | 743 ms |
コンパイル使用メモリ | 86,700 KB |
実行使用メモリ | 7,296 KB |
最終ジャッジ日時 | 2024-07-04 22:12:26 |
合計ジャッジ時間 | 2,684 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#include <iostream>#include <algorithm>#include <tuple>#include <vector>#include <string>#include <queue>#include <cmath>#include <set>#include <map>using namespace std;using ll = long long;int main(){int n;cin >> n;vector<int> a(n);vector<pair<int, int>> lower(n), upper(n);for(int i = 0; i< n; i++){cin >> a[i];if(i==0){lower[0].first = a[i];lower[0].second = a[i];}else if(i==1){lower[i].first = lower[i-1].first;lower[i].second = lower[i-1].second;}else{lower[i].first = min(lower[i-1].first, a[i-1]);lower[i].second = max(lower[i-1].second, a[i-1]);}}for(int i = n-1; i >= 0; i--){if(i==n-1){upper[i].first = a[i];upper[i].second = a[i];}else if(i==n-2){upper[i].first = a[i+1];upper[i].second = a[i+1];}else{upper[i].first = min(upper[i+1].first, a[i+1]);upper[i].second = max(upper[i+1].second, a[i+1]);}}int mn = 1001001001;for(int i = 1; i< n-1; i++){if(a[i]> lower[i].first && a[i] > upper[i].first){mn = min(mn, a[i]+ lower[i].first + upper[i].first);}else if(a[i] < lower[i].first && a[i] < upper[i].first){mn = min(mn, a[i] + lower[i].first + upper[i].first);}}if(mn==1001001001){cout << -1 << endl;return 0;}cout << mn << endl;return 0;}