結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー snrnsidy
提出日時 2021-05-13 03:33:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 386 ms / 2,000 ms
コード長 818 bytes
コンパイル時間 2,233 ms
コンパイル使用メモリ 201,052 KB
最終ジャッジ日時 2025-01-21 10:24:18
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	long long int res = 1e18;
	int n;
	long long int t;
	set <long long int> l, r;
	vector <long long int> v;

	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> t;
		v.push_back(t);
		r.insert(t);
	}

	for (int i = 0; i < n; i++)
	{
		r.erase(v[i]);
		if (l.size() > 0 && r.size() > 0)
		{
			long long int a = *l.begin();
			long long int b = *r.begin();
			if (a < v[i] && b < v[i])
			{
				res = min(res, a + b + v[i]);
			}
			auto it1 = l.lower_bound(v[i]);
			auto it2 = r.lower_bound(v[i]);
			if (it1 != l.end() && it2 != r.end())
			{
				a = *it1;
				b = *it2;
				res = min(res, a + b + v[i]);
			}
		}
		l.insert(v[i]);
	}

	if (res >= 1e18)
	{
		res = -1;
	}

	cout << res << '\n';

	return 0;
}
0