結果
| 問題 | No.1095 Smallest Kadomatsu Subsequence |
| コンテスト | |
| ユーザー |
minato
|
| 提出日時 | 2020-06-26 21:26:30 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 70 ms / 2,000 ms |
| コード長 | 2,600 bytes |
| 記録 | |
| コンパイル時間 | 4,312 ms |
| コンパイル使用メモリ | 235,292 KB |
| 最終ジャッジ日時 | 2025-01-11 10:45:43 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
ソースコード
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using u64 = uint_fast64_t;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln = '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; }
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; }
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
void print() { cout << "\n"; }
template<class T, class... Args>
void print(const T &x, const Args &... args) {
cout << x << " ";
print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename T>
struct SegmentTree {
const T inf = numeric_limits<T>::max();
int N;
vector<T> node;
SegmentTree() = default;
SegmentTree(vector<T> &v) {init(v);}
void init(vector<T> &v) {
int sz = v.size();
N = 1; while (N < sz) N *= 2;
node.assign(2*N,inf);
for (int i = 0; i < sz; i++) node[i+N] = v[i];
for (int i = N-1; i > 0; i--) node[i] = min(node[i<<1|0],node[i<<1|1]);
}
void update(int x, T val) { // (0-indexed)
x += N;
node[x] = val;
while (x > 1) {
x >>= 1;
node[x] = min(node[x<<1|0],node[x<<1|1]);
}
}
T get(int l, int r) { // [l,r) (0-indexed)
T res = inf;
l += N; r += N;
while (l < r) {
if (l & 1) res = min(res,node[l++]);
l >>= 1;
if (r & 1) res = min(res,node[--r]);
r >>= 1;
}
return res;
}
T operator[](int x) {return node[x+N];}
};
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int N; cin >> N;
vector<int> A(N);
rep(i,N) cin >> A[i];
SegmentTree<int> seg(A);
int ans = 1e9;
for (int i = 1; i < N-1; i++) {
int l = seg.get(0,i);
int r = seg.get(i+1,N);
if (l < A[i] and r < A[i]) {
chmin(ans,l+A[i]+r);
}
if (l > A[i] and r > A[i]) {
chmin(ans,l+A[i]+r);
}
}
cout << (ans==1e9 ? -1 : ans) << ln;
}
minato