結果

問題 No.1077 Noelちゃんと星々4
ユーザー tentententen
提出日時 2024-01-25 15:21:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 1,841 bytes
コンパイル時間 2,847 ms
コンパイル使用メモリ 86,296 KB
実行使用メモリ 92,716 KB
最終ジャッジ日時 2024-01-25 15:21:41
合計ジャッジ時間 6,933 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
54,752 KB
testcase_01 AC 79 ms
55,016 KB
testcase_02 AC 129 ms
79,784 KB
testcase_03 AC 142 ms
88,980 KB
testcase_04 AC 105 ms
64,764 KB
testcase_05 AC 102 ms
64,784 KB
testcase_06 AC 115 ms
68,124 KB
testcase_07 AC 118 ms
68,228 KB
testcase_08 AC 111 ms
68,112 KB
testcase_09 AC 102 ms
64,676 KB
testcase_10 AC 172 ms
88,212 KB
testcase_11 AC 126 ms
78,080 KB
testcase_12 AC 120 ms
71,560 KB
testcase_13 AC 103 ms
60,656 KB
testcase_14 AC 143 ms
87,048 KB
testcase_15 AC 118 ms
71,572 KB
testcase_16 AC 110 ms
68,100 KB
testcase_17 AC 124 ms
79,912 KB
testcase_18 AC 146 ms
89,000 KB
testcase_19 AC 101 ms
60,512 KB
testcase_20 AC 68 ms
54,764 KB
testcase_21 AC 144 ms
92,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[][] dp = new int[n + 1][10001];
        for (int i = 1; i <= n; i++) {
            int min = Integer.MAX_VALUE;
            int x = sc.nextInt();
            for (int j = 0; j <= 10000; j++) {
                min = Math.min(min, dp[i - 1][j]);
                dp[i][j] = min + Math.abs(x - j);
            }
        }
        System.out.println(Arrays.stream(dp[n]).min().getAsInt());
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0