結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー risujirohrisujiroh
提出日時 2020-06-26 21:45:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 243 ms / 2,000 ms
コード長 1,968 bytes
コンパイル時間 2,270 ms
コンパイル使用メモリ 210,400 KB
実行使用メモリ 6,440 KB
最終ジャッジ日時 2023-09-18 03:54:11
合計ジャッジ時間 5,856 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 11 ms
4,376 KB
testcase_14 AC 10 ms
4,376 KB
testcase_15 AC 10 ms
4,384 KB
testcase_16 AC 10 ms
4,380 KB
testcase_17 AC 11 ms
4,380 KB
testcase_18 AC 10 ms
4,376 KB
testcase_19 AC 11 ms
4,376 KB
testcase_20 AC 10 ms
4,376 KB
testcase_21 AC 11 ms
4,376 KB
testcase_22 AC 11 ms
4,380 KB
testcase_23 AC 241 ms
6,144 KB
testcase_24 AC 243 ms
6,196 KB
testcase_25 AC 243 ms
6,196 KB
testcase_26 AC 239 ms
6,144 KB
testcase_27 AC 240 ms
6,208 KB
testcase_28 AC 82 ms
6,368 KB
testcase_29 AC 82 ms
6,276 KB
testcase_30 AC 164 ms
6,440 KB
testcase_31 AC 159 ms
6,204 KB
testcase_32 AC 161 ms
6,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define DEBUG(...)
#endif

template <class T> struct segtree {
  const int n;
  vector<T> t;
  segtree(int _n = 0) : n(_n), t(2 * n) {}
  T& operator[](int i) { return t[n + i]; }
  void build() { for (int i = n; i--; ) t[i] = t[2 * i] * t[2 * i + 1]; }
  T fold(int l, int r) const {
    T a, b;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) a = a * t[l++];
      if (r & 1) b = t[--r] * b;
    }
    return a * b;
  }
  void set(int i, T a) {
    t[i += n] = a;
    while (i >>= 1) t[i] = t[2 * i] * t[2 * i + 1];
  }
};

template <class T> constexpr T inf = numeric_limits<T>::max() / 2.1;

template <class T> struct mini {
  T v = inf<T>;
  mini operator*(mini b) const {
    return b.v < v ? b : *this;
  }
};
template <class T> struct maxi {
  T v = -inf<T>;
  maxi operator*(maxi b) const {
    return v < b.v ? b : *this;
  }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto&& e : a) cin >> e;
  int res = inf<int>;
  {
    vector<int> ord(n);
    iota(begin(ord), end(ord), 0);
    sort(begin(ord), end(ord), [&](int i, int j) {
      return a[i] < a[j];
    });
    segtree<mini<int>> seg(n);
    for (int i : ord) {
      if (seg.fold(0, i).v != inf<int> and seg.fold(i + 1, n).v != inf<int>) {
        res = min(res, seg.fold(0, i).v + a[i] + seg.fold(i + 1, n).v);
      }
      seg.set(i, {a[i]});
    }
  }
  {
    vector<int> ord(n);
    iota(begin(ord), end(ord), 0);
    sort(begin(ord), end(ord), [&](int i, int j) {
      return a[i] > a[j];
    });
    segtree<mini<int>> seg(n);
    for (int i : ord) {
      if (seg.fold(0, i).v != inf<int> and seg.fold(i + 1, n).v != inf<int>) {
        res = min(res, seg.fold(0, i).v + a[i] + seg.fold(i + 1, n).v);
      }
      seg.set(i, {a[i]});
    }
  }
  if (res == inf<int>) {
    res = -1;
  }
  cout << res << '\n';
}
0