結果

問題 No.636 硬貨の枚数2
ユーザー imulanimulan
提出日時 2018-01-30 12:46:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,294 bytes
コンパイル時間 1,491 ms
コンパイル使用メモリ 166,588 KB
実行使用メモリ 4,820 KB
最終ジャッジ日時 2023-08-28 16:38:08
合計ジャッジ時間 4,462 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,596 KB
testcase_01 AC 4 ms
4,580 KB
testcase_02 AC 5 ms
4,584 KB
testcase_03 AC 4 ms
4,592 KB
testcase_04 AC 5 ms
4,540 KB
testcase_05 AC 4 ms
4,636 KB
testcase_06 AC 5 ms
4,596 KB
testcase_07 AC 5 ms
4,580 KB
testcase_08 AC 5 ms
4,580 KB
testcase_09 AC 5 ms
4,532 KB
testcase_10 AC 4 ms
4,596 KB
testcase_11 AC 5 ms
4,728 KB
testcase_12 AC 4 ms
4,592 KB
testcase_13 AC 5 ms
4,516 KB
testcase_14 AC 4 ms
4,564 KB
testcase_15 AC 5 ms
4,820 KB
testcase_16 AC 4 ms
4,704 KB
testcase_17 AC 5 ms
4,704 KB
testcase_18 AC 4 ms
4,640 KB
testcase_19 AC 4 ms
4,588 KB
testcase_20 AC 5 ms
4,564 KB
testcase_21 AC 4 ms
4,576 KB
testcase_22 AC 5 ms
4,632 KB
testcase_23 AC 5 ms
4,608 KB
testcase_24 AC 4 ms
4,756 KB
testcase_25 AC 4 ms
4,452 KB
testcase_26 AC 4 ms
4,580 KB
testcase_27 AC 5 ms
4,516 KB
testcase_28 AC 4 ms
4,700 KB
testcase_29 AC 4 ms
4,524 KB
testcase_30 AC 4 ms
4,520 KB
testcase_31 AC 4 ms
4,480 KB
testcase_32 AC 5 ms
4,504 KB
testcase_33 AC 4 ms
4,652 KB
testcase_34 AC 4 ms
4,640 KB
testcase_35 AC 4 ms
4,496 KB
testcase_36 AC 4 ms
4,584 KB
testcase_37 AC 4 ms
4,516 KB
testcase_38 AC 4 ms
4,600 KB
testcase_39 AC 4 ms
4,516 KB
testcase_40 AC 4 ms
4,528 KB
testcase_41 AC 4 ms
4,464 KB
testcase_42 AC 4 ms
4,600 KB
testcase_43 AC 4 ms
4,456 KB
testcase_44 AC 3 ms
4,468 KB
testcase_45 AC 3 ms
4,500 KB
testcase_46 AC 3 ms
4,456 KB
testcase_47 AC 4 ms
4,516 KB
testcase_48 AC 3 ms
4,760 KB
testcase_49 AC 3 ms
4,584 KB
testcase_50 AC 3 ms
4,476 KB
testcase_51 AC 3 ms
4,604 KB
testcase_52 AC 3 ms
4,456 KB
testcase_53 AC 4 ms
4,740 KB
testcase_54 AC 5 ms
4,540 KB
testcase_55 AC 4 ms
4,536 KB
testcase_56 AC 5 ms
4,584 KB
testcase_57 AC 4 ms
4,516 KB
testcase_58 AC 5 ms
4,532 KB
testcase_59 AC 5 ms
4,704 KB
testcase_60 AC 4 ms
4,576 KB
testcase_61 AC 5 ms
4,628 KB
testcase_62 AC 5 ms
4,812 KB
testcase_63 AC 4 ms
4,580 KB
testcase_64 AC 3 ms
4,516 KB
testcase_65 AC 3 ms
4,604 KB
testcase_66 AC 4 ms
4,480 KB
testcase_67 AC 3 ms
4,564 KB
testcase_68 AC 4 ms
4,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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