結果

問題 No.838 Noelちゃんと星々3
ユーザー GrenacheGrenache
提出日時 2019-07-01 10:51:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 624 ms / 2,000 ms
コード長 1,257 bytes
コンパイル時間 3,630 ms
コンパイル使用メモリ 75,452 KB
実行使用メモリ 63,052 KB
最終ジャッジ日時 2023-09-22 18:13:11
合計ジャッジ時間 11,926 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 610 ms
63,052 KB
testcase_01 AC 622 ms
62,460 KB
testcase_02 AC 609 ms
62,688 KB
testcase_03 AC 624 ms
62,340 KB
testcase_04 AC 611 ms
62,744 KB
testcase_05 AC 132 ms
55,828 KB
testcase_06 AC 124 ms
56,116 KB
testcase_07 AC 127 ms
56,368 KB
testcase_08 AC 132 ms
55,832 KB
testcase_09 AC 131 ms
55,800 KB
testcase_10 AC 126 ms
55,676 KB
testcase_11 AC 132 ms
56,148 KB
testcase_12 AC 133 ms
56,552 KB
testcase_13 AC 130 ms
56,324 KB
testcase_14 AC 126 ms
56,120 KB
testcase_15 AC 131 ms
56,164 KB
testcase_16 AC 121 ms
56,372 KB
testcase_17 AC 122 ms
53,948 KB
testcase_18 AC 132 ms
56,276 KB
testcase_19 AC 130 ms
55,976 KB
testcase_20 AC 122 ms
55,980 KB
testcase_21 AC 120 ms
55,800 KB
testcase_22 AC 122 ms
54,228 KB
testcase_23 AC 121 ms
55,800 KB
testcase_24 AC 121 ms
55,844 KB
testcase_25 AC 120 ms
55,948 KB
testcase_26 AC 121 ms
56,080 KB
testcase_27 AC 123 ms
56,160 KB
testcase_28 AC 123 ms
55,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder838 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		final long INF = Long.MAX_VALUE / 2;

		int n = sc.nextInt();

		int[] y = new int[n];
		for (int i = 0; i < n; i++) {
			y[i] = sc.nextInt();
		}
		Arrays.sort(y);

		long[] dp2 = new long[n + 1];
		long[] dp3 = new long[n + 1];
		Arrays.fill(dp2, INF);
		Arrays.fill(dp3, INF);
		dp2[0] = dp3[0] = 0;
		for (int i = 0; i < n; i++) {
			if (i + 1 - 2 >= 0) {
				dp2[i + 1] = Math.min(dp2[i + 1], dp2[i + 1 - 2] + y[i] - y[i - 1]);
				dp2[i + 1] = Math.min(dp2[i + 1], dp3[i + 1 - 2] + y[i] - y[i - 1]);
			}
			if (i + 1 - 3 >= 0) {
				dp3[i + 1] = Math.min(dp3[i + 1], dp3[i + 1 - 3] + y[i] - y[i - 1] + y[i - 1] - y[i - 2]);
				dp3[i + 1] = Math.min(dp3[i + 1], dp2[i + 1 - 3] + y[i] - y[i - 1] + y[i - 1] - y[i - 2]);
			}
		}
		
		pr.println(Math.min(dp2[n], dp3[n]));
	}
	
	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);
			
		solve();
			
		pr.close();
		sc.close();
	}

	static class Printer extends PrintWriter {
		Printer(OutputStream out) {
			super(out);
		}
	}
}
0