結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー ks2mks2m
提出日時 2020-06-26 21:48:57
言語 Java19
(openjdk 21)
結果
AC  
実行時間 1,221 ms / 2,000 ms
コード長 1,421 bytes
コンパイル時間 2,864 ms
コンパイル使用メモリ 75,552 KB
実行使用メモリ 89,688 KB
最終ジャッジ日時 2023-09-18 04:07:09
合計ジャッジ時間 15,865 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
50,020 KB
testcase_01 AC 43 ms
50,156 KB
testcase_02 AC 44 ms
49,496 KB
testcase_03 AC 45 ms
49,404 KB
testcase_04 AC 47 ms
49,588 KB
testcase_05 AC 46 ms
49,516 KB
testcase_06 AC 46 ms
49,504 KB
testcase_07 AC 47 ms
49,404 KB
testcase_08 AC 47 ms
49,548 KB
testcase_09 AC 47 ms
49,512 KB
testcase_10 AC 47 ms
49,612 KB
testcase_11 AC 48 ms
49,736 KB
testcase_12 AC 46 ms
49,404 KB
testcase_13 AC 170 ms
55,316 KB
testcase_14 AC 168 ms
55,560 KB
testcase_15 AC 163 ms
55,984 KB
testcase_16 AC 167 ms
55,600 KB
testcase_17 AC 162 ms
55,472 KB
testcase_18 AC 165 ms
55,628 KB
testcase_19 AC 169 ms
55,604 KB
testcase_20 AC 169 ms
56,148 KB
testcase_21 AC 167 ms
55,784 KB
testcase_22 AC 162 ms
55,344 KB
testcase_23 AC 1,221 ms
88,504 KB
testcase_24 AC 1,109 ms
89,688 KB
testcase_25 AC 1,110 ms
88,256 KB
testcase_26 AC 1,131 ms
88,208 KB
testcase_27 AC 1,134 ms
89,392 KB
testcase_28 AC 617 ms
83,968 KB
testcase_29 AC 629 ms
84,580 KB
testcase_30 AC 980 ms
88,484 KB
testcase_31 AC 977 ms
88,516 KB
testcase_32 AC 974 ms
88,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.TreeMap;
import java.util.TreeSet;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] sa = br.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}
		br.close();

		TreeMap<Integer, Integer> map = new TreeMap<>();
		for (int i = 0; i < n; i++) {
			map.put(a[i], i);
		}

		TreeSet<Integer> set = new TreeSet<>();
		boolean h = false;
		int ans = Integer.MAX_VALUE;
		for (Integer k : map.keySet()) {
			if (set.size() == 0) {
			} else if (set.size() == 1) {
				if (set.last() < map.get(k)) {
					h = true;
				}
			} else {
				if (h && set.last() > map.get(k)) {
					int f = set.pollFirst();
					int v1 = a[f];
					int v2 = a[map.get(k)];
					int v3 = a[set.higher(map.get(k))];
					ans = Math.min(ans, v1 + v2 + v3);
					set.add(f);
				}
				if (!h && set.first() < map.get(k)) {
					int f = set.pollLast();
					int v1 = a[f];
					int v2 = a[map.get(k)];
					int v3 = a[set.lower(map.get(k))];
					ans = Math.min(ans, v1 + v2 + v3);
					set.add(f);
				}
			}
			set.add(map.get(k));
		}
		if (ans == Integer.MAX_VALUE) {
			System.out.println(-1);
		} else {
			System.out.println(ans);
		}
	}
}
0