結果

問題 No.636 硬貨の枚数2
ユーザー nobuta05
提出日時 2022-04-01 01:41:22
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,777 bytes
コンパイル時間 715 ms
コンパイル使用メモリ 100,156 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-22 14:45:09
合計ジャッジ時間 2,763 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.string, std.conv;
import std.algorithm, std.math;
import std.range;
import std.container.rbtree, std.container.dlist;
import std.container.binaryheap, std.container.array;
import std.typecons;

alias mstring = char[];
const long INF = 1L << 60L;
const long mod = 1_000_000_000 + 7;

void chmin (T)(ref T x, T y) {
  x = min(x, y);
}

void chmax (T)(ref T x, T y) {
  x = max(x, y);
}

// 単一の数値を取得
// readln.chomp.to!int;
//  or
// int a;
// readf("%s\n", a);

// 複数の数値を取得(可変数個の場合推奨)
// readln.chomp.split.map!(to!long).array

// 複数の数値を取得(固定数個の場合、推奨)
// int a, b;
// readf("%s %s\n", &a, &b);

// インデントを一個ずらして複数の数値を取得(累積和とかで1-indexedの方が良い時がある)
// long[] vs = readln.chomp.split.map!(to!long).array;
// vs = [0L] ~ vs;

// 小数点は以下で指定
// double ret = 10.0;
// writefln("%.12f", ret);

// 配列の最後の要素は arr[$-1] でアクセスできる

void main () {
  mstring N = readln.chomp.dup;
  N.reverse;
  N = N ~ "0";
  const int maxK = N.length.to!int;

  auto dp = new long[][](maxK + 1, 2);
  foreach (i; 0 .. maxK + 1)
    foreach (j; 0 .. 2)
      dp[i][j] = INF;

  dp[maxK][0] = 0L;
  for (int i = maxK; i > 0; i--) {
    foreach (j; 0 .. 2) {
      if (dp[i][j] == INF)
        continue;

      foreach (ai; 0L .. 10L) {
        int ni = N[maxK - i] - '0' + j;

        int nj = cast(int)(ai < ni);
        long a = (ai / 5L) + (ai - (ai / 5L) * 5L);
        long b = ai - ni;
        if (b < 0L)
          b += 10L;
        b = (b / 5L) + (b - (b / 5L) * 5L);
        chmin(dp[i - 1][nj], dp[i][j] + a + b);
      }
    }
  }

  long ret = dp[0][0];
  writeln(ret);
}
0