結果

問題 No.1077 Noelちゃんと星々4
ユーザー tentententen
提出日時 2020-08-21 08:30:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 264 ms / 2,000 ms
コード長 747 bytes
コンパイル時間 2,846 ms
コンパイル使用メモリ 76,756 KB
実行使用メモリ 97,028 KB
最終ジャッジ日時 2024-04-22 02:35:32
合計ジャッジ時間 6,995 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
41,152 KB
testcase_01 AC 123 ms
41,344 KB
testcase_02 AC 228 ms
75,136 KB
testcase_03 AC 234 ms
76,768 KB
testcase_04 AC 171 ms
55,856 KB
testcase_05 AC 152 ms
51,880 KB
testcase_06 AC 176 ms
55,820 KB
testcase_07 AC 185 ms
57,284 KB
testcase_08 AC 171 ms
56,112 KB
testcase_09 AC 162 ms
52,068 KB
testcase_10 AC 246 ms
76,376 KB
testcase_11 AC 220 ms
66,428 KB
testcase_12 AC 204 ms
66,544 KB
testcase_13 AC 157 ms
52,428 KB
testcase_14 AC 233 ms
75,808 KB
testcase_15 AC 195 ms
66,732 KB
testcase_16 AC 176 ms
56,312 KB
testcase_17 AC 199 ms
66,284 KB
testcase_18 AC 237 ms
77,336 KB
testcase_19 AC 153 ms
52,020 KB
testcase_20 AC 109 ms
41,040 KB
testcase_21 AC 264 ms
97,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static final int MAX = 10000;
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
		    arr[i] = sc.nextInt();
		}
		int[][] dp = new int[n][MAX + 1];
		for (int i = MAX; i >= 0; i--) {
		    dp[n - 1][i] = Math.abs(i - arr[n - 1]);
		}
		for (int i = n - 2; i >= 0; i--) {
		    int min = dp[i + 1][MAX];
		    for (int j = MAX; j >= 0; j--) {
		        min = Math.min(min, dp[i + 1][j]);
		        dp[i][j] = Math.abs(j - arr[i]) + min;
		    }
		}
		int min = Integer.MAX_VALUE;
		for (int i = 0; i <= MAX; i++) {
		    min = Math.min(min, dp[0][i]);
		}
		System.out.println(min);
	}
}
0