結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー どららどらら
提出日時 2020-06-26 22:24:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 284 ms / 2,000 ms
コード長 1,932 bytes
コンパイル時間 2,065 ms
コンパイル使用メモリ 175,772 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2023-09-18 05:45:18
合計ジャッジ時間 6,187 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 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 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 11 ms
4,380 KB
testcase_14 AC 11 ms
4,380 KB
testcase_15 AC 11 ms
4,380 KB
testcase_16 AC 11 ms
4,384 KB
testcase_17 AC 12 ms
4,380 KB
testcase_18 AC 11 ms
4,376 KB
testcase_19 AC 12 ms
4,376 KB
testcase_20 AC 11 ms
4,380 KB
testcase_21 AC 12 ms
4,376 KB
testcase_22 AC 11 ms
4,376 KB
testcase_23 AC 280 ms
8,744 KB
testcase_24 AC 284 ms
8,944 KB
testcase_25 AC 282 ms
8,808 KB
testcase_26 AC 281 ms
8,744 KB
testcase_27 AC 279 ms
8,788 KB
testcase_28 AC 200 ms
8,796 KB
testcase_29 AC 200 ms
8,800 KB
testcase_30 AC 233 ms
8,796 KB
testcase_31 AC 232 ms
8,944 KB
testcase_32 AC 233 ms
9,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

static const int INF = 999999999;
class RMQ {
  int N;
  std::vector<int> dat;

 public:
  RMQ() {}
  void init(int n_) {
    N = 1;
    while (N < n_) N *= 2;
    for (int i = 0; i < 2 * N - 1; i++)
      dat.push_back(INF);
  }
  int query(int a, int b) { return _query(a, b, 0, 0, N); }
  int _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return INF;
    if (a <= l && r <= b) return dat[k];
    else {
      int vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);

      if (vl < vr) return vl;
      else return vr;
    }
  }
  void update(int i, int x) {
    i += N - 1;
    dat[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      if (dat[i * 2 + 1] < dat[i * 2 + 2]) {
        dat[i] = dat[i * 2 + 1];
      } else {
        dat[i] = dat[i * 2 + 2];
      }
    }
  }
};

int main(){
  int N;
  cin >> N;
  vector<pair<int,int>> v;
  rep(i,N){
    int a;
    cin >> a;
    v.emplace_back(a,i);
  }
  sort(ALLOF(v));

  int ret = INF;
  {
    RMQ rmq;
    rmq.init(N+5);
    rep(i,v.size()){
      int x = v[i].first;
      int idx = v[i].second;
      int lhs = rmq.query(0,idx);
      int rhs = rmq.query(idx+1, N);
      ret = min(ret, lhs + x + rhs);
      rmq.update(idx, x);
    }
  }
  {
    RMQ rmq;
    rmq.init(N+5);
    for(int i=v.size()-1; i>=0; i--){
      int x = v[i].first;
      int idx = v[i].second;
      int lhs = rmq.query(0,idx);
      int rhs = rmq.query(idx+1, N);
      ret = min(ret, lhs + x + rhs);
      rmq.update(idx, x);
    }
  }

  if(ret == INF) cout << -1 << endl;
  else cout << ret << endl;

  return 0;
}

0