結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー 夜
提出日時 2020-08-20 02:40:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 368 ms / 2,000 ms
コード長 1,325 bytes
コンパイル時間 1,234 ms
コンパイル使用メモリ 100,516 KB
実行使用メモリ 15,360 KB
最終ジャッジ日時 2024-04-20 18:40:45
合計ジャッジ時間 5,537 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 11 ms
6,940 KB
testcase_14 AC 11 ms
6,944 KB
testcase_15 AC 10 ms
6,940 KB
testcase_16 AC 11 ms
6,940 KB
testcase_17 AC 11 ms
6,940 KB
testcase_18 AC 12 ms
6,944 KB
testcase_19 AC 10 ms
6,944 KB
testcase_20 AC 10 ms
6,944 KB
testcase_21 AC 10 ms
6,944 KB
testcase_22 AC 10 ms
6,944 KB
testcase_23 AC 341 ms
15,360 KB
testcase_24 AC 331 ms
15,360 KB
testcase_25 AC 352 ms
15,360 KB
testcase_26 AC 368 ms
15,360 KB
testcase_27 AC 326 ms
15,232 KB
testcase_28 AC 211 ms
15,232 KB
testcase_29 AC 210 ms
15,360 KB
testcase_30 AC 281 ms
15,360 KB
testcase_31 AC 276 ms
15,180 KB
testcase_32 AC 273 ms
15,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
int a[200020];
int ml[200020], mr[200020];
int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	int nl = 1000000007, nr = 1000000007;
	set<int> stl, str;
	for (int i = 0; i < n; i++) {
		if (nl > a[i]) {
			nl = a[i];
		}
		if (nr > a[n - i - 1]) {
			nr = a[n - i - 1];
		}
		ml[i] = nl;
		mr[n - i - 1] = nr;
		if (i > 1) {
			str.insert(a[i]);
		}
	}
	stl.insert(a[0]);
	int ans = 1000000007;
	for (int i = 1; i < n - 1; i++) {
		bool bo = true, bo1 = true;
		if (a[i] <= ml[i - 1] || a[i] <= mr[i + 1]) {
			bo = false;
		}
		auto itr = stl.set::upper_bound(a[i]);
		auto itr1 = str.set::upper_bound(a[i]);
		if (itr == stl.end() || itr1 == str.end()) {
			bo1 = false;
		}
		if (bo) {
			if (ans > ml[i - 1] + a[i] + mr[i + 1]) {
				ans = ml[i - 1] + a[i] + mr[i + 1];
			}
		}
		if (bo1) {
			if (ans > *itr + a[i] + *itr1) {
				ans = *itr + a[i] + *itr1;
			}
		}
		str.erase(a[i + 1]);
		stl.insert(a[i]);
	}
	if (ans == 1000000007) {
		cout << "-1" << endl;
		return 0;
	}
	cout << ans << endl;
}
0