結果

問題 No.884 Eat and Add
ユーザー k0hn0yutal2l5k0hn0yutal2l5
提出日時 2019-09-13 22:23:07
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 8 ms / 1,000 ms
コード長 818 bytes
コンパイル時間 780 ms
コンパイル使用メモリ 52,552 KB
実行使用メモリ 5,108 KB
最終ジャッジ日時 2023-09-17 14:42:32
合計ジャッジ時間 1,321 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 7 ms
4,972 KB
testcase_04 AC 7 ms
4,948 KB
testcase_05 AC 8 ms
5,036 KB
testcase_06 AC 7 ms
4,972 KB
testcase_07 AC 7 ms
5,108 KB
testcase_08 AC 7 ms
4,960 KB
testcase_09 AC 6 ms
5,020 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>

using namespace std;

int dp[200000][2];

int main()
{
    string S;
    cin >> S;
    if (S[S.size() - 1] == '0')
    {
        dp[0][0] = 0;
        dp[0][1] = 1;
    }
    else
    {
        dp[0][0] = 1;
        dp[0][1] = 0;
    }
    //cout << dp[0][0] << dp[0][1] << endl;

    for (int i = 1; i < S.size(); i++)
    {
        if (S[S.size() - i - 1] == '0')
        {
            dp[i][0] = min(dp[i - 1][0], min(i, dp[i - 1][1] + 2));
            dp[i][1] = min(dp[i - 1][0] + 1, dp[i - 1][1] + 1);
        }
        else
        {
            dp[i][0] = min(dp[i - 1][0] + 1, dp[i - 1][1] + 2);
            dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]);
        }
        //cout << dp[i][0] << dp[i][1] << endl;
    }
    cout << min(dp[S.size() - 1][0], dp[S.size() - 1][1] + 2) << endl;
}
0