結果
問題 |
No.884 Eat and Add
|
ユーザー |
|
提出日時 | 2019-09-14 06:00:40 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 13 ms / 1,000 ms |
コード長 | 792 bytes |
コンパイル時間 | 660 ms |
コンパイル使用メモリ | 72,000 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-05 03:16:06 |
合計ジャッジ時間 | 1,514 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 9 |
ソースコード
#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; }