結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー takumattakumat
提出日時 2020-07-05 08:59:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 384 ms / 2,000 ms
コード長 1,676 bytes
コンパイル時間 956 ms
コンパイル使用メモリ 109,984 KB
実行使用メモリ 17,308 KB
最終ジャッジ日時 2023-10-22 01:55:14
合計ジャッジ時間 6,982 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 10 ms
4,480 KB
testcase_14 AC 11 ms
4,480 KB
testcase_15 AC 11 ms
4,480 KB
testcase_16 AC 11 ms
4,480 KB
testcase_17 AC 11 ms
4,480 KB
testcase_18 AC 11 ms
4,480 KB
testcase_19 AC 10 ms
4,480 KB
testcase_20 AC 10 ms
4,480 KB
testcase_21 AC 11 ms
4,480 KB
testcase_22 AC 10 ms
4,480 KB
testcase_23 AC 384 ms
17,308 KB
testcase_24 AC 359 ms
17,308 KB
testcase_25 AC 361 ms
17,308 KB
testcase_26 AC 354 ms
17,308 KB
testcase_27 AC 369 ms
17,308 KB
testcase_28 AC 221 ms
17,308 KB
testcase_29 AC 241 ms
17,308 KB
testcase_30 AC 282 ms
17,308 KB
testcase_31 AC 282 ms
17,308 KB
testcase_32 AC 277 ms
17,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>

typedef unsigned long long ULLONG;
typedef long long LLONG;
static const LLONG MOD_NUM = 1000000007;//998244353;

template<class _T> static void getval(_T& a) {
	std::cin >> a;
}
template<class _T> static void getval(_T& a, _T& b) {
	std::cin >> a >> b;
}
template<class _T> static void getval(_T& a, _T& b, _T& c) {
	std::cin >> a >> b >> c;
}

static void exec();

int main()
{
	exec();
	fflush(stdout);
	return 0;
}

static void exec()
{
	int N;
	getval(N);

	std::vector<LLONG> ai(N), minL(N + 1, MOD_NUM), minR(N + 1, MOD_NUM);
	LLONG minVal = MOD_NUM;
	std::set<LLONG> left, right;
	for (int i = 0; i < N; i++) {
		getval(ai[i]);

		minL[i + 1] = minL[i];
		if (minVal > ai[i]) {
			minVal = ai[i];
			minL[i + 1] = minVal;
		}
		right.insert(ai[i]);
	}

	minVal = MOD_NUM;
	for (int i = N - 1; i > 0; i--) {
		minR[i] = minR[i + 1];
		if (minVal > ai[i]) {
			minVal = ai[i];
			minR[i] = minVal;
		}
	}

	LLONG ans = MOD_NUM, pta = 0, ptb = 0;
	left.insert(ai[0]);
	right.erase(ai[0]);
	for (int i = 1; i < N - 1; i++) {
		if ((minL[i] < ai[i]) && (ai[i] > minR[i])) {
			pta = minL[i] + ai[i] + minR[i];
			if (ans > pta) {
				ans = pta;
			}
		}

		right.erase(ai[i]);
		auto lm = left.lower_bound(ai[i]);
		auto rm = right.lower_bound(ai[i]);
		if (lm != left.end() && rm != right.end()) {
			ptb = *lm + ai[i] + *rm;
			if (ans > ptb) {
				ans = ptb;
			}
		}
		left.insert(ai[i]);
	}

	if (ans >= MOD_NUM) ans = -1;
	printf("%lld\n", ans);
}
0