結果

問題 No.1687 What the Heck?
ユーザー ks2mks2m
提出日時 2021-09-24 21:27:07
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 664 bytes
コンパイル時間 2,104 ms
コンパイル使用メモリ 73,724 KB
実行使用メモリ 76,508 KB
最終ジャッジ日時 2023-09-18 20:39:19
合計ジャッジ時間 14,412 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
55,816 KB
testcase_01 AC 121 ms
55,572 KB
testcase_02 AC 121 ms
55,568 KB
testcase_03 WA -
testcase_04 AC 122 ms
55,924 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 659 ms
66,016 KB
testcase_10 AC 550 ms
60,484 KB
testcase_11 AC 536 ms
61,140 KB
testcase_12 AC 1,069 ms
74,648 KB
testcase_13 AC 1,103 ms
75,104 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.TreeSet;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] p = new int[n];
		for (int i = 0; i < n; i++) {
			p[i] = sc.nextInt();
		}
		sc.close();

		TreeSet<Integer> set = new TreeSet<>();
		for (int i = 1; i <= n; i++) {
			set.add(i);
		}

		long ans = 0;
		for (int i = n - 1; i >= 0; i--) {
			Integer h = set.higher(p[i]);
			if (h != null) {
				set.remove(h);
				ans += i + 1;
			} else {
				Integer l = set.pollFirst();
				if (l != p[i]) {
					ans -= i + 1;
				}
			}
		}
		System.out.println(ans);
	}
}
0