結果
| 問題 |
No.1095 Smallest Kadomatsu Subsequence
|
| コンテスト | |
| ユーザー |
KoD
|
| 提出日時 | 2020-06-26 21:39:56 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,562 bytes |
| コンパイル時間 | 2,316 ms |
| コンパイル使用メモリ | 111,728 KB |
| 最終ジャッジ日時 | 2025-01-11 11:04:52 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 WA * 11 |
ソースコード
#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
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::vector<i32> prefmax(N), prefmin(N), suffmax(N), suffmin(N);
for (auto i: range(0, N)) {
prefmax[i] = A[i];
prefmin[i] = A[i];
if (i > 0) {
chmax(prefmax[i], prefmax[i - 1]);
chmin(prefmin[i], prefmin[i - 1]);
}
}
for (auto i: revrange(0, N)) {
suffmax[i] = A[i];
suffmin[i] = A[i];
if (i + 1 < N) {
chmax(suffmax[i], suffmax[i + 1]);
chmin(suffmin[i], suffmin[i + 1]);
}
}
i32 ans = inf32;
for (auto i: range(1, N - 1)) {
if (prefmax[i - 1] > A[i] && suffmax[i + 1] > A[i]) {
chmin(ans, prefmax[i - 1] + A[i] + suffmax[i + 1]);
}
if (prefmin[i - 1] < A[i] && suffmin[i + 1] < A[i]) {
chmin(ans, prefmin[i - 1] + A[i] + suffmin[i + 1]);
}
}
std::cout << (ans == inf32 ? -1 : ans) << '\n';
return 0;
}
KoD