結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー takumattakumat
提出日時 2020-07-05 05:19:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,641 bytes
コンパイル時間 1,087 ms
コンパイル使用メモリ 109,256 KB
実行使用メモリ 13,612 KB
最終ジャッジ日時 2023-10-21 18:02:47
合計ジャッジ時間 4,448 ms
ジャッジサーバーID
(参考情報)
judge9 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 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 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 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 7 ms
4,348 KB
testcase_14 AC 7 ms
4,348 KB
testcase_15 AC 7 ms
4,348 KB
testcase_16 AC 7 ms
4,348 KB
testcase_17 AC 7 ms
4,348 KB
testcase_18 AC 7 ms
4,348 KB
testcase_19 AC 6 ms
4,348 KB
testcase_20 AC 7 ms
4,348 KB
testcase_21 AC 7 ms
4,348 KB
testcase_22 AC 7 ms
4,348 KB
testcase_23 AC 192 ms
13,080 KB
testcase_24 AC 160 ms
11,232 KB
testcase_25 AC 171 ms
9,912 KB
testcase_26 AC 184 ms
11,232 KB
testcase_27 AC 159 ms
9,912 KB
testcase_28 AC 164 ms
13,608 KB
testcase_29 AC 168 ms
13,608 KB
testcase_30 WA -
testcase_31 AC 172 ms
13,608 KB
testcase_32 AC 172 ms
13,608 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);

	int INF = 300000001;
	int minVal = INF;
	int minIdx = -1;
	std::vector<int> ai(N);
	for (int i = 0; i < N; i++) {
		getval(ai[i]);

		if (minVal > ai[i]) {
			minVal = ai[i];
			minIdx = i;
		}
	}

	LLONG ans = 0;
	int fMin = INF, fPeak = INF, fFst = INF;
	std::set<int> fs;
	for (int i = minIdx - 1; i >= 0; i--) {
		if (fMin > ai[i]) {
			fMin = ai[i];
		}
		if (fFst > ai[i]) {
			auto f = fs.lower_bound(ai[i]);
			if (f != fs.end()) {
				fFst = ai[i];
				fPeak = *f;
			}
		}
		fs.insert(ai[i]);
	}

	fs.erase(fs.begin(), fs.end());
	int lMin = INF, lPeak = INF, lLast = INF;
	for (int i = minIdx + 1; i < N; i++) {
		if (lMin > ai[i]) {
			lMin = ai[i];
		}

		if (lLast > ai[i]) {
			auto l = fs.lower_bound(ai[i]);
			if (l != fs.end()) {
				lLast = ai[i];
				lPeak = *l;
			}
		}
		fs.insert(ai[i]);
	}
	ans = std::min({ fMin + minVal + lMin, fFst + fPeak + minVal, minVal + lPeak + lLast });
	if (ans > INF) ans = -1;
	printf("%lld\n", ans);
}
0