結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー KoDKoD
提出日時 2020-06-26 21:46:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 327 ms / 2,000 ms
コード長 2,818 bytes
コンパイル時間 770 ms
コンパイル使用メモリ 87,600 KB
実行使用メモリ 16,384 KB
最終ジャッジ日時 2023-09-18 04:00:25
合計ジャッジ時間 5,383 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 10 ms
4,380 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 10 ms
4,380 KB
testcase_18 AC 10 ms
4,376 KB
testcase_19 AC 10 ms
4,380 KB
testcase_20 AC 10 ms
4,380 KB
testcase_21 AC 10 ms
4,380 KB
testcase_22 AC 10 ms
4,376 KB
testcase_23 AC 327 ms
16,160 KB
testcase_24 AC 312 ms
16,376 KB
testcase_25 AC 323 ms
16,384 KB
testcase_26 AC 320 ms
16,216 KB
testcase_27 AC 311 ms
16,204 KB
testcase_28 AC 235 ms
16,372 KB
testcase_29 AC 236 ms
16,244 KB
testcase_30 AC 257 ms
16,316 KB
testcase_31 AC 258 ms
16,316 KB
testcase_32 AC 260 ms
16,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <set>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { lhs = rhs; return true; }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { lhs = rhs; return true; }
  return false;
}

struct range {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) { }
    constexpr void operator ++ () noexcept { ++i; }
    constexpr itr operator * () const noexcept { return i; }
    constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) { }
  constexpr iterator begin() const noexcept { return l; }
  constexpr iterator end() const noexcept { return r; }
};

struct revrange {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) { }
    constexpr void operator ++ () noexcept { --i; }
    constexpr itr operator * () const noexcept { return i; }
    constexpr bool operator != (iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr revrange(itr l_, itr r_) noexcept : l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr iterator begin() const noexcept { return r; }
  constexpr iterator end() const noexcept { return l; }
};

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

int main() {
  size_t N;
  std::cin >> N;
  std::vector<i32> A(N);
  for (auto &x: A) {
    std::cin >> x;
  }
  std::set<i32> set;
  std::vector<i32> prefmin(N, inf32), prefmax(N, inf32);
  for (auto i: range(0, N)) {
    if (!set.empty()) {
      auto min = *set.begin();
      if (min < A[i]) {
        prefmin[i] = min;
      }
      auto itr = set.upper_bound(A[i]);
      if (itr != set.end()) {
        prefmax[i] = *itr;
      }
    }
    set.insert(A[i]);
  }
  set.clear();
  std::vector<i32> suffmin(N, inf32), suffmax(N, inf32);
  for (auto i: revrange(0, N)) {
    if (!set.empty()) {
      auto min = *set.begin();
      if (min < A[i]) {
        suffmin[i] = min;
      }
      auto itr = set.upper_bound(A[i]);
      if (itr != set.end()) {
        suffmax[i] = *itr;
      }
    }
    set.insert(A[i]);
  }
  i32 ans = inf32;
  for (auto i: range(0, N)) {
    if (prefmin[i] != inf32 && suffmin[i] != inf32) {
      chmin(ans, prefmin[i] + A[i] + suffmin[i]);
    }
    if (prefmax[i] != inf32 && suffmax[i] != inf32) {
      chmin(ans, prefmax[i] + A[i] + suffmax[i]);
    }
  }
  std::cout << (ans == inf32 ? -1 : ans) << '\n';
  return 0;
}
0