結果

問題 No.884 Eat and Add
ユーザー erbowlerbowl
提出日時 2020-03-20 10:32:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 674 bytes
コンパイル時間 1,528 ms
コンパイル使用メモリ 168,944 KB
実行使用メモリ 14,228 KB
最終ジャッジ日時 2023-08-20 21:32:08
合計ジャッジ時間 2,551 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 23 ms
14,224 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 23 ms
14,176 KB
testcase_07 AC 22 ms
14,224 KB
testcase_08 AC 22 ms
14,220 KB
testcase_09 AC 22 ms
14,108 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 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');

    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