結果

問題 No.636 硬貨の枚数2
ユーザー しらっ亭しらっ亭
提出日時 2017-05-10 17:43:34
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 861 bytes
コンパイル時間 631 ms
コンパイル使用メモリ 65,308 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-19 10:22:37
合計ジャッジ時間 2,549 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <assert.h>

using namespace std;

const vector<int> Z = {0,1,2,3,2,1,2,3,3,2,1};
const vector<int> C = {0,1,2,5,6,7};
const vector<int> X = {0,1,2,1,2,3};

int solve_dp(string s) {
  int n = (int) s.size();

  const int inf = 1e9;

  vector<vector<int>> dp(n, vector<int>(2, inf));

  int d0 = s[0] - '0';
  dp[0][0] = Z[d0];
  dp[0][1] = Z[d0+1];

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 2; j++) {
      int d = s[i] - '0' + j;
      for (int k = 0; k < 6; k++) {
        if (d + C[k] >= 10) {
          dp[i][j] = min(dp[i][j], dp[i-1][1] + X[k] + Z[d + C[k] - 10]);
        }
        else {
          dp[i][j] = min(dp[i][j], dp[i-1][0] + X[k] + Z[d + C[k]]);
        }
      }
    }
  }

  return dp[n-1][0];
}

int main() {
  string s;
  cin >> s;
  cout << solve_dp(s) << endl;
  return 0;
}
0