結果

問題 No.636 硬貨の枚数2
コンテスト
ユーザー 37zigen
提出日時 2018-01-21 14:27:48
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 930 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,848 ms
コンパイル使用メモリ 83,420 KB
実行使用メモリ 44,256 KB
最終ジャッジ日時 2026-05-31 13:50:40
合計ジャッジ時間 9,022 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

class Main {
	int[] p = { 0, 1, 2, 3, 4, 1, 2, 3, 4, 5 };

	public void run() {
		Scanner sc = new Scanner(System.in);
		String str = sc.next();
		int n = str.length();
		int[][] a = new int[n + 2][2];
		for (int i = 0; i < a.length; ++i)
			for (int j = 0; j < a[i].length; ++j)
				a[i][j] = Integer.MAX_VALUE / 3;
		a[0][0] = 0;
		for (int i = 0; i <= n; ++i) {
			int v = i == n ? 0 : str.charAt(n - 1 - i) - '0';
			for (int j = 0; j <= 9; ++j) {
				for (int k = 0; k <= 1; ++k) {
					a[i + 1][(v + j + k) / 10] = Math.min(a[i + 1][(v + j + k) / 10],
							a[i][k] + p[(v + j + k) % 10] + p[j]);
				}
			}
		}
		System.out.println(a[n + 1][0]);
	}

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

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