結果

問題 No.636 硬貨の枚数2
コンテスト
ユーザー imulan
提出日時 2018-01-30 12:46:39
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 1,294 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,139 ms
コンパイル使用メモリ 182,340 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-06-03 15:15:28
合計ジャッジ時間 4,193 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const int N = 10010;
const int INF = 19191919;

inline int cost(int x){
    if(x<5) return x;
    return 1+x-5;
}

string n;
// (桁, 以上?, 繰り下がり?)
int dp[N][2][2];
int dfs(int d, int l, int s){
    if(d==0){
        if(s==0 && l==1) return 0;
        return INF;
    }
    if(dp[d][l][s]>=0) return dp[d][l][s];

    int ret = INF;
    rep(i,10){
        int diff = i - (n[d]-'0'+s);
        int nl = (diff>=0);
        int ns = 0;
        if(diff<0){
            ns = 1;
            diff += 10;
        }
        ret = min(ret, dfs(d-1,nl,ns)+cost(i)+cost(diff));
    }
    // printf(" %d %d %d :: %d\n",d,l,s,ret);
    return dp[d][l][s] = ret;
}

int main(){
    cin >>n;
    while(n.size()!=N) n = "0"+n;

    memset(dp,-1,sizeof(dp));
    cout << dfs(N-1,1,0) << endl;
    return 0;
}
0