結果

問題 No.884 Eat and Add
ユーザー iqueue02
提出日時 2020-06-27 10:51:19
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 23 ms / 1,000 ms
コード長 858 bytes
コンパイル時間 1,807 ms
コンパイル使用メモリ 199,428 KB
最終ジャッジ日時 2025-01-11 12:39:51
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
constexpr int INF = 2e9;

int main() {
  string s;
  cin >> s;
  s = '0' + s;
  reverse(s.begin(), s.end());
  int n  = s.size();
  vector<vector<int>> dp(n + 1, vector<int>(2, INF));
  dp[0][0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2; j++) {
      int d = s[i] - '0';
      int now = d + j;
      int nj = 0;
      if (now == 2) {
        now = 0;
        nj = 1;
      }

      if (!now) dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j]);
      else {
        dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j] + 1);
        if (!nj) dp[i + 1][nj + 1] = min(dp[i + 1][nj + 1], dp[i][j] + 1);
      }
    }

  }

  cout << dp[n][0] << endl;
  return 0;
} 
0