結果

問題 No.884 Eat and Add
ユーザー yuppe19 😺yuppe19 😺
提出日時 2019-09-14 06:00:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 1,000 ms
コード長 792 bytes
コンパイル時間 708 ms
コンパイル使用メモリ 71,960 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 12:24:14
合計ジャッジ時間 1,339 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

constexpr int inf = 987'654'321;

int f(string s) {
  s = '0' + s;
  int n = static_cast<int>(s.size());
  // dp[繰り上がったか] := 最小回数
  vector<int> dp(2, inf);
  dp[0] = 0;
  for(int i=n-1; i>=0; --i) { // 下から
    vector<int> ndp(2, inf);
    for(int carry=0; carry<2; ++carry) {
      if(dp[carry] == inf) { continue; }
      for(int d : {-1, 0, 1}) {
        int val    = (carry + d + s[i]-'0') % 2,
            ncarry = (carry + d + s[i]-'0') / 2;
        if(val != 0) { continue; }
        ndp[ncarry] = min(ndp[ncarry], dp[carry] + abs(d));
      }
    }
    dp = ndp;
  }
  int res = dp[0];
  return res;
}

int main(void) {
  string s; cin >> s;
  int res = f(s);
  printf("%d\n", res);
  return 0;
}
0