結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー heno239heno239
提出日時 2020-06-26 21:34:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 256 ms / 2,000 ms
コード長 2,444 bytes
コンパイル時間 1,366 ms
コンパイル使用メモリ 123,832 KB
実行使用メモリ 8,192 KB
最終ジャッジ日時 2023-09-18 03:18:03
合計ジャッジ時間 5,131 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 11 ms
4,376 KB
testcase_14 AC 11 ms
4,380 KB
testcase_15 AC 11 ms
4,376 KB
testcase_16 AC 10 ms
4,376 KB
testcase_17 AC 10 ms
4,376 KB
testcase_18 AC 10 ms
4,380 KB
testcase_19 AC 10 ms
4,380 KB
testcase_20 AC 11 ms
4,380 KB
testcase_21 AC 10 ms
4,376 KB
testcase_22 AC 10 ms
4,380 KB
testcase_23 AC 253 ms
8,192 KB
testcase_24 AC 256 ms
7,992 KB
testcase_25 AC 255 ms
7,980 KB
testcase_26 AC 254 ms
7,912 KB
testcase_27 AC 253 ms
8,060 KB
testcase_28 AC 164 ms
8,048 KB
testcase_29 AC 163 ms
8,048 KB
testcase_30 AC 199 ms
7,924 KB
testcase_31 AC 199 ms
8,044 KB
testcase_32 AC 199 ms
7,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);


struct SegT {
private:
	int sz; vector<int> node;
	const int init_c = mod;
public:
	SegT(int n) {
		sz = 1;
		while (sz < n)sz *= 2;
		node.resize(2 * sz - 1, init_c);
	}
	int f(int a, int b) {
		return min(a, b);
	}
	void update(int k, int a) {
		k += sz - 1;
		node[k] = a;
		while (k > 0) {
			k = (k - 1) / 2;
			node[k] = f(node[k * 2 + 1], node[k * 2 + 2]);
		}
	}
	int query(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r < 0)r = sz;
		if (r <= a || b <= l)return init_c;
		else if (a <= l && r <= b)return node[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);
			return f(vl, vr);
		}
	}
};
void solve() {
	int n; cin >> n;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	SegT st(n);
	int ans = mod;

	vector<P> vs;
	rep(i, n)vs.push_back({ a[i],i });
	sort(all(vs));
	
	rep(i, n) {
		int id = vs[i].second;

		int le = st.query(0, id);
		int ri = st.query(id + 1, n);

		ans = min(ans, le + ri + a[id]);

		st.update(id, a[id]);
	}

	rep(i, n)st.update(i, mod);

	per(i, n) {
		int id = vs[i].second;
		int le = st.query(0, id);
		int ri = st.query(id + 1, n);

		ans = min(ans, le + ri + a[id]);

		st.update(id, a[id]);
	}
	if (ans >= mod)ans = -1;
	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(7);
	//init_f();
	//init();
	//int t; cin >> t; rep(i, t)
	solve();
	stop
		return 0;
}
0