結果
| 問題 | No.636 硬貨の枚数2 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2018-02-04 18:21:36 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 4 ms / 2,000 ms | 
| コード長 | 1,142 bytes | 
| コンパイル時間 | 873 ms | 
| コンパイル使用メモリ | 104,124 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-06-30 04:00:48 | 
| 合計ジャッジ時間 | 2,504 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 65 | 
ソースコード
#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;
const int INF = INT_MAX / 2;
int solve(int x)
{
    if(x < 0)
        return INF;
    else if(x < 5)
        return x;
    else
        return x - 4;
}
int main()
{
    string s;
    cin >> s;
    int n = s.size();
    vector<int> dp(2, 0);
    dp[1] = 1;
    for(int i=0; i<n; ++i){
        vector<int> nextDp(2, INF);
        for(int a=0; a<2; ++a){
            for(int b=0; b<10; ++b){
                int x = a * 10 + b - (s[i] - '0');
                nextDp[0] = min(nextDp[0], dp[a] + solve(b) + solve(x));
                nextDp[1] = min(nextDp[1], dp[a] + solve(b) + solve(x - 1));
            }
        }
        dp.swap(nextDp);
    }
    cout << dp[0] << endl;
    return 0;
}
            
            
            
        