結果

問題 No.884 Eat and Add
ユーザー betrue12betrue12
提出日時 2019-09-13 21:36:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 1,000 ms
コード長 767 bytes
コンパイル時間 1,588 ms
コンパイル使用メモリ 169,484 KB
実行使用メモリ 14,384 KB
最終ジャッジ日時 2023-09-17 07:00:40
合計ジャッジ時間 2,408 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 23 ms
14,188 KB
testcase_04 AC 23 ms
14,384 KB
testcase_05 AC 22 ms
14,256 KB
testcase_06 AC 22 ms
14,240 KB
testcase_07 AC 22 ms
14,192 KB
testcase_08 AC 22 ms
14,188 KB
testcase_09 AC 21 ms
14,256 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void chmin(int& a, int b){
    a = min(a, b);
}

int main(){
    string N;
    cin >> N;
    reverse(N.begin(), N.end());
    int L = N.size();
    vector<vector<int>> dp(L+1, vector<int>(2, 1e9));
    dp[0][0] = 0;
    for(int i=0; i<L; i++) for(int j=0; j<2; j++){
        int c = j + N[i] - '0';
        if(c == 0){
            chmin(dp[i+1][0], dp[i][j]);
            chmin(dp[i+1][1], dp[i][j] + 2);
        }else if(c == 1){
            chmin(dp[i+1][0], dp[i][j] + 1);
            chmin(dp[i+1][1], dp[i][j] + 1);
        }else{
            chmin(dp[i+1][0], dp[i][j] + 2);
            chmin(dp[i+1][1], dp[i][j]);
        }
    }
    int ans = min(dp[L][0], dp[L][1]+1);
    cout << ans << endl;
    return 0;
}
0