結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー tsushimatsushima
提出日時 2020-06-27 12:02:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 244 ms / 2,000 ms
コード長 1,260 bytes
コンパイル時間 1,957 ms
コンパイル使用メモリ 205,852 KB
実行使用メモリ 25,880 KB
最終ジャッジ日時 2023-09-18 19:16:41
合計ジャッジ時間 5,769 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 9 ms
4,580 KB
testcase_14 AC 9 ms
4,564 KB
testcase_15 AC 9 ms
4,524 KB
testcase_16 AC 8 ms
4,624 KB
testcase_17 AC 9 ms
4,688 KB
testcase_18 AC 9 ms
4,748 KB
testcase_19 AC 9 ms
4,380 KB
testcase_20 AC 8 ms
4,688 KB
testcase_21 AC 8 ms
4,488 KB
testcase_22 AC 9 ms
4,512 KB
testcase_23 AC 234 ms
25,732 KB
testcase_24 AC 244 ms
25,736 KB
testcase_25 AC 244 ms
25,792 KB
testcase_26 AC 240 ms
25,880 KB
testcase_27 AC 234 ms
25,668 KB
testcase_28 AC 195 ms
25,728 KB
testcase_29 AC 194 ms
25,868 KB
testcase_30 AC 216 ms
25,740 KB
testcase_31 AC 220 ms
25,756 KB
testcase_32 AC 213 ms
25,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, 0, n) cin >> a[i];

  vector<int> min_l(n + 1, 1e9), min_r(n + 1, 1e9);
  rep(i, 0, n) { min_l[i + 1] = min(min_l[i], a[i]); }
  repr(i, 0, n) { min_r[i] = min(min_r[i + 1], a[i]); }

  int ans = 1e9;

  rep(i, 1, n - 1) {
    if (min_l[i] > a[i] || min_r[i + 1] > a[i])
      continue;
    chmin(ans, a[i] + min_l[i] + min_r[i + 1]);
  }

  vector<int> max_l(n+1,1e9), max_r(n+1,1e9);
  set<int> st1, st2;
  rep(i,0,n-1){
    auto it = st1.upper_bound(a[i]);
    if (it != st1.end())max_l[i]=*it;
    st1.insert(a[i]);
  }

  repr(i,1,n){
    auto it = st2.upper_bound(a[i]);
    if (it !=st2.end()) max_r[i+1]=*it;
    st2.insert(a[i]);
  }

  int ans2 = 1e9;
  rep(i,1,n){
    chmin(ans2, a[i] + max_l[i] + max_r[i + 1]);
  }

  chmin(ans, ans2);
  if (ans == 1e9)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
0