結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー leaf_1415leaf_1415
提出日時 2020-06-26 21:26:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 370 ms / 2,000 ms
コード長 1,386 bytes
コンパイル時間 781 ms
コンパイル使用メモリ 79,708 KB
実行使用メモリ 20,848 KB
最終ジャッジ日時 2023-09-18 02:58:49
合計ジャッジ時間 5,197 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,456 KB
testcase_01 AC 2 ms
7,596 KB
testcase_02 AC 2 ms
7,444 KB
testcase_03 AC 3 ms
7,456 KB
testcase_04 AC 3 ms
7,604 KB
testcase_05 AC 3 ms
7,548 KB
testcase_06 AC 3 ms
7,548 KB
testcase_07 AC 2 ms
7,464 KB
testcase_08 AC 2 ms
7,484 KB
testcase_09 AC 2 ms
7,500 KB
testcase_10 AC 2 ms
7,480 KB
testcase_11 AC 2 ms
7,508 KB
testcase_12 AC 3 ms
7,532 KB
testcase_13 AC 10 ms
8,228 KB
testcase_14 AC 9 ms
8,228 KB
testcase_15 AC 10 ms
8,188 KB
testcase_16 AC 10 ms
8,364 KB
testcase_17 AC 10 ms
8,432 KB
testcase_18 AC 9 ms
8,200 KB
testcase_19 AC 10 ms
8,388 KB
testcase_20 AC 10 ms
8,476 KB
testcase_21 AC 10 ms
8,240 KB
testcase_22 AC 10 ms
8,240 KB
testcase_23 AC 358 ms
20,560 KB
testcase_24 AC 360 ms
20,572 KB
testcase_25 AC 370 ms
20,596 KB
testcase_26 AC 366 ms
20,564 KB
testcase_27 AC 359 ms
20,572 KB
testcase_28 AC 175 ms
20,848 KB
testcase_29 AC 176 ms
20,616 KB
testcase_30 AC 222 ms
20,556 KB
testcase_31 AC 228 ms
20,620 KB
testcase_32 AC 226 ms
20,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<llint, llint> P;

llint n;
llint a[200005];
set<llint> S;
llint l[200005], l2[200005];
llint r[200005], r2[200005];

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		l[i] = l2[i] = r[i] = r2[i] = inf;
	}
	
	for(int i = 1; i <= n; i++){
		if(S.size()) l[i] = *S.begin();
		auto it = S.upper_bound(a[i]);
		if(it != S.end()) l2[i] = *it;
		S.insert(a[i]);
	}
	
	S.clear();
	for(int i = n; i >= 1; i--){
		if(S.size()) r[i] = *S.begin();
		auto it = S.upper_bound(a[i]);
		if(it != S.end()) r2[i] = *it;
		S.insert(a[i]);
	}
	
	llint ans = inf;
	for(int i = 1; i <= n; i++){
		if(l[i] < a[i] && r[i] < a[i]) ans = min(ans, l[i]+a[i]+r[i]);
		ans = min(ans, l2[i]+a[i]+r2[i]);
	}
	if(ans > inf/2) ans = -1;
	
	cout << ans << endl;
	
	return 0;
}
0