結果

問題 No.636 硬貨の枚数2
ユーザー はまやんはまやん
提出日時 2018-01-22 16:29:55
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 2,121 bytes
コンパイル時間 1,870 ms
コンパイル使用メモリ 167,636 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-26 00:51:35
合計ジャッジ時間 4,289 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/





string N;
//---------------------------------------------------------------------------------------------------
int g[11] = { 0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1 };
//int g[11] = { 0, 1, 2, 3, 2, 1, 2, 3, 4, 5, 6 };
int memo[10101][2];
int f(int i, int j) { // =[0,i]円+j円を支払う最小枚数
    if (i < 0) {
        if(j == 0) return 0;
        else return 1;
    }
    if (0 <= memo[i][j]) return memo[i][j];

    int res = inf;
    rep(d, 0, 10) {
        int n = N[i] - '0' + j;

        int sm = g[d];
        if (d < n) sm += f(i - 1, 1) + g[10 + d - n];
        else sm += f(i - 1, 0) + g[d - n];

        chmin(res, sm);
    }

    return memo[i][j] = res;
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;
    rep(i, 0, 10101) rep(j, 0, 2) memo[i][j] = -1;
    cout << f(N.length() - 1, 0) << endl;
}
0