結果

問題 No.884 Eat and Add
ユーザー erbowlerbowl
提出日時 2020-03-20 11:01:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 1,000 ms
コード長 728 bytes
コンパイル時間 1,531 ms
コンパイル使用メモリ 169,548 KB
実行使用メモリ 14,448 KB
最終ジャッジ日時 2023-08-20 21:33:09
合計ジャッジ時間 2,362 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

int main() {
    string s;
    std::cin >> s;
    ll n = s.length();
    // i桁目までみたときのコスト、jは繰り上がっているかフラグ
    vector<vector<ll>> dp(n+1,vector<ll>(2,0));

    dp[0][0]=dp[0][1]=(s[n-1]=='1');
    if(s[n-1]=='0'){
        dp[0][1]=1e10;
    }
    
    for (int i = 1; i < n; i++) {
        ll now = n-i-1;
        if(s[now]=='1'){
            dp[i][1] = min(dp[i-1][0]+1,dp[i-1][1]);
            dp[i][0] = dp[i-1][0]+1;
        }else{
            dp[i][1] = dp[i-1][1]+1;
            dp[i][0] = min(dp[i-1][0],dp[i-1][1]+1);
        }
    }
    
    std::cout << min(dp[n-1][1]+1,dp[n-1][0]) << std::endl;
}
0