結果

問題 No.636 硬貨の枚数2
ユーザー beetbeet
提出日時 2018-01-19 23:24:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,421 bytes
コンパイル時間 1,201 ms
コンパイル使用メモリ 147,744 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-08-26 00:34:23
合計ジャッジ時間 3,612 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 4 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 4 ms
4,376 KB
testcase_35 AC 4 ms
4,380 KB
testcase_36 AC 4 ms
4,376 KB
testcase_37 AC 4 ms
4,376 KB
testcase_38 AC 3 ms
4,376 KB
testcase_39 AC 4 ms
4,380 KB
testcase_40 AC 3 ms
4,376 KB
testcase_41 AC 4 ms
4,380 KB
testcase_42 AC 3 ms
4,376 KB
testcase_43 AC 3 ms
4,376 KB
testcase_44 AC 4 ms
4,376 KB
testcase_45 AC 3 ms
4,376 KB
testcase_46 AC 3 ms
4,380 KB
testcase_47 AC 3 ms
4,376 KB
testcase_48 AC 3 ms
4,376 KB
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 3 ms
4,376 KB
testcase_51 AC 4 ms
4,376 KB
testcase_52 AC 3 ms
4,380 KB
testcase_53 AC 3 ms
4,376 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 2 ms
4,376 KB
testcase_56 AC 2 ms
4,500 KB
testcase_57 AC 2 ms
4,376 KB
testcase_58 AC 2 ms
4,380 KB
testcase_59 AC 2 ms
4,376 KB
testcase_60 AC 2 ms
4,380 KB
testcase_61 AC 2 ms
4,376 KB
testcase_62 AC 2 ms
4,376 KB
testcase_63 AC 2 ms
4,380 KB
testcase_64 AC 2 ms
4,376 KB
testcase_65 AC 3 ms
4,376 KB
testcase_66 AC 4 ms
4,376 KB
testcase_67 AC 2 ms
4,376 KB
testcase_68 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;

template<typename T> void chmin(T &a,T b){if(a>b) a=b;}
template<typename T> void chmax(T &a,T b){if(a<b) a=b;}

const Int INF = 1e18;
Int dp[2][11234];
Int dp2[2][11234];
signed main(){
  string s;
  cin>>s;
  while(s.back()=='0') s.pop_back();
  for(Int i=0;i<2;i++)
    for(Int j=0;j<11234;j++)
      dp[i][j]=dp2[i][j]=INF;

  Int n=s.size();
  string t;
  for(Int i=0;i<n;i++)
    t+=string(1,9-(s[i]-'0')+'0');
  t.back()++;
  //cout<<s<<":"<<t<<endl;
  
  auto calc=[](Int x){return x/10+((x%10)/5)+(x%5);};
  dp2[0][n]=0;
  for(Int i=n;i>0;i--){
    for(Int k=0;k<10;k++){
      Int x=t[i-1]-'0'+k;
      chmin(dp2[x/10][i-1],dp2[0][i]+calc(k)+calc(x%10));
      chmin(dp2[(x+1)/10][i-1],dp2[1][i]+calc(k)+calc((x+1)%10));
    }
  }
  
  dp[0][0]=0;
  Int ans=1e18;
  for(Int k=1;k<10;k++){
    chmin(ans,calc(k)+calc(k-1)+dp2[0][0]);
    chmin(ans,calc(k)+calc(k-1+1)+dp2[1][0]);
  }
  //cout<<ans<<" "<<dp2[0][0]<<" "<<dp2[1][0]<<endl;
  for(Int i=0;i<n;i++){
    chmin(dp[0][i+1],dp[0][i]+calc(s[i]-'0'));
    //continue;
    //if(i+1==n) continue;
    for(Int k=(s[i]-'0'+1);k<10;k++){
      chmin(ans,dp[0][i]+calc(k)+calc(k-(s[i]-'0'+(i!=n-1)))+dp2[0][i+1]);
      chmin(ans,dp[0][i]+calc(k)+calc(k-(s[i]-'0'+(i!=n-1))+1)+dp2[1][i+1]);
    }
  }
  //cout<<ans<<" "<<dp[0][n]<<endl;
  chmin(ans,dp[0][n]);
  cout<<ans<<endl;
  return 0;
}
0