結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー lasjg72lasjg72
提出日時 2020-06-26 22:27:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 1,549 bytes
コンパイル時間 846 ms
コンパイル使用メモリ 85,008 KB
実行使用メモリ 7,264 KB
最終ジャッジ日時 2023-09-18 05:52:03
合計ジャッジ時間 2,950 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 4 ms
4,376 KB
testcase_23 AC 71 ms
6,940 KB
testcase_24 AC 71 ms
6,996 KB
testcase_25 AC 71 ms
7,052 KB
testcase_26 AC 72 ms
7,124 KB
testcase_27 AC 72 ms
7,036 KB
testcase_28 AC 73 ms
7,104 KB
testcase_29 AC 72 ms
7,132 KB
testcase_30 AC 72 ms
6,952 KB
testcase_31 AC 72 ms
7,264 KB
testcase_32 AC 71 ms
7,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0