結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー simansiman
提出日時 2022-09-25 17:05:50
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 1,752 ms / 2,000 ms
コード長 1,803 bytes
コンパイル時間 2,393 ms
コンパイル使用メモリ 105,728 KB
実行使用メモリ 9,448 KB
最終ジャッジ日時 2023-08-24 00:59:53
合計ジャッジ時間 19,897 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 9 ms
4,380 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 9 ms
4,376 KB
testcase_16 AC 9 ms
4,380 KB
testcase_17 AC 8 ms
4,376 KB
testcase_18 AC 8 ms
4,380 KB
testcase_19 AC 8 ms
4,380 KB
testcase_20 AC 9 ms
4,376 KB
testcase_21 AC 9 ms
4,380 KB
testcase_22 AC 9 ms
4,380 KB
testcase_23 AC 1,741 ms
9,388 KB
testcase_24 AC 1,741 ms
9,440 KB
testcase_25 AC 1,742 ms
9,392 KB
testcase_26 AC 1,739 ms
9,444 KB
testcase_27 AC 1,740 ms
9,448 KB
testcase_28 AC 1,728 ms
9,396 KB
testcase_29 AC 1,733 ms
9,344 KB
testcase_30 AC 1,732 ms
9,420 KB
testcase_31 AC 1,752 ms
9,436 KB
testcase_32 AC 1,747 ms
9,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <climits>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  vector<int> L(N);
  vector<int> LA;
  vector<int> LL(N, -1);
  vector<int> R(N);
  vector<int> RA;
  vector<int> RR(N, -1);
  int min_v = INT_MAX;

  for (int i = 0; i <= N - 1; ++i) {
    int a = A[i];
    auto it = lower_bound(LA.begin(), LA.end(), a);
    int idx = it - LA.begin();

    if (it != LA.end()) {
      LL[i] = a + LA[idx];
    }
    LA.insert(LA.begin() + idx, a);

    min_v = min(min_v, a);
    L[i] = min_v;
  }

  min_v = INT_MAX;
  for (int i = N - 1; i >= 0; --i) {
    int a = A[i];
    auto it = lower_bound(RA.begin(), RA.end(), a);
    int idx = it - RA.begin();

    if (it != RA.end()) {
      RR[i] = a + RA[idx];
    }
    RA.insert(RA.begin() + idx, a);

    min_v = min(min_v, a);
    R[i] = min_v;
  }

  /*
  for (int i = 0; i < N; ++i) {
    cerr << LL[i] << " ";
  }
  cerr << endl;
  for (int i = 0; i < N; ++i) {
    cerr << RR[i] << " ";
  }
  cerr << endl;
  */

  int ans = INT_MAX;

  for (int i = 1; i <= N - 2; ++i) {
    int a = A[i];
    int l = LL[i];
    int r = RR[i];
    if (l == -1) continue;
    if (r == -1) continue;

    int v = l + r - a;
    ans = min(ans, v);
  }

  for (int i = 1; i <= N - 2; ++i) {
    int a = A[i];
    int l = L[i - 1];
    int r = R[i + 1];
    if (l > a) continue;
    if (r > a) continue;

    int v = a + l + r;
    ans = min(ans, v);
  }


  if (ans == INT_MAX) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}
0