結果

問題 No.838 Noelちゃんと星々3
ユーザー 37zigen37zigen
提出日時 2019-06-15 03:25:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 653 ms / 2,000 ms
コード長 784 bytes
コンパイル時間 2,871 ms
コンパイル使用メモリ 77,852 KB
実行使用メモリ 61,672 KB
最終ジャッジ日時 2024-04-27 23:03:44
合計ジャッジ時間 10,628 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 653 ms
59,492 KB
testcase_01 AC 636 ms
59,856 KB
testcase_02 AC 608 ms
61,264 KB
testcase_03 AC 552 ms
60,456 KB
testcase_04 AC 601 ms
61,672 KB
testcase_05 AC 118 ms
54,240 KB
testcase_06 AC 101 ms
53,164 KB
testcase_07 AC 115 ms
53,856 KB
testcase_08 AC 121 ms
54,200 KB
testcase_09 AC 125 ms
54,196 KB
testcase_10 AC 110 ms
53,536 KB
testcase_11 AC 127 ms
54,148 KB
testcase_12 AC 128 ms
54,476 KB
testcase_13 AC 119 ms
54,080 KB
testcase_14 AC 102 ms
53,024 KB
testcase_15 AC 126 ms
54,204 KB
testcase_16 AC 114 ms
54,120 KB
testcase_17 AC 113 ms
54,036 KB
testcase_18 AC 127 ms
54,216 KB
testcase_19 AC 118 ms
54,080 KB
testcase_20 AC 115 ms
53,784 KB
testcase_21 AC 102 ms
53,108 KB
testcase_22 AC 110 ms
54,072 KB
testcase_23 AC 115 ms
53,772 KB
testcase_24 AC 103 ms
53,044 KB
testcase_25 AC 112 ms
54,144 KB
testcase_26 AC 117 ms
54,104 KB
testcase_27 AC 101 ms
53,188 KB
testcase_28 AC 104 ms
52,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[] Y = new long[N];
		for (int i = 0; i < N; ++i) {
			Y[i] = sc.nextLong();
		}
		Arrays.sort(Y);

		long[] dp = new long[N];
		Arrays.fill(dp, Long.MAX_VALUE / 3);
		for (int i = 1; i < N; ++i) {
			dp[i] = Math.min(dp[i], (i >= 2 ? dp[i - 2] : 0) + Math.abs(Y[i] - Y[i - 1]));
			if (i >= 2)
				dp[i] = Math.min(dp[i],
						(i >= 3 ? dp[i - 3] : 0) + Math.abs(Y[i] - Y[i - 1]) + Math.abs(Y[i - 1] - Y[i - 2]));
		}
		System.out.println(dp[N - 1]);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0