結果

問題 No.636 硬貨の枚数2
ユーザー htensaihtensai
提出日時 2020-02-13 11:05:18
言語 Java
(openjdk 23)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 2,020 bytes
コンパイル時間 3,034 ms
コンパイル使用メモリ 83,832 KB
実行使用メモリ 54,380 KB
最終ジャッジ日時 2024-10-05 09:11:42
合計ジャッジ時間 12,746 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] arr = sc.next().toCharArray();
        int length = arr.length + 2;
        int[] inputs = new int[length];
        for (int i = 1; i < length - 1; i++) {
            inputs[i] = arr[i - 1] - '0';
        }
        int[][] dp = new int[length][2];
        dp[0][0] = 0;
        dp[0][1] = Integer.MAX_VALUE / 10;
        for (int i = 1; i < length; i++) {
            int x = inputs[length - i - 1];
            dp[i][0] = Math.min(getCount(x) + dp[i - 1][0], getCount(x + 1) + dp[i - 1][1]);
            dp[i][1] = Math.min(getReturn(x) + dp[i - 1][0], getReturn(x + 1) + dp[i - 1][1]);
        }
        System.out.println(dp[length - 1][0]);
    }
    
    static int getCount(int x) {
        switch (x) {
            case 0:
                return 0;
            case 1:
                return 1;
            case 2:
                return 2;
            case 3:
                return 3;
            case 4:
                return 2;
            case 5:
                return 1;
            case 6:
                return 2;
            case 7:
                return 3;
            case 8:
                return 4;
            case 9:
                return 3;
            case 10:
                return 1;
        }
        return 0;
    }
    
    static int getReturn(int x) {
        switch (x) {
            case 0:
                return Integer.MAX_VALUE / 10;
            case 1:
                return 5;
            case 2:
                return 4;
            case 3:
                return 3;
            case 4:
                return 2;
            case 5:
                return 1;
            case 6:
                return 2;
            case 7:
                return 3;
            case 8:
                return 2;
            case 9:
                return 1;
            case 10:
                return 0;
        }
        return 0;
    }
}
0