結果
| 問題 | No.636 硬貨の枚数2 | 
| コンテスト | |
| ユーザー |  beet | 
| 提出日時 | 2018-01-19 23:24:34 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 5 ms / 2,000 ms | 
| コード長 | 1,421 bytes | 
| コンパイル時間 | 1,721 ms | 
| コンパイル使用メモリ | 161,256 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-12-24 12:53:22 | 
| 合計ジャッジ時間 | 3,422 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 65 | 
ソースコード
#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;
}
            
            
            
        