結果
問題 | No.884 Eat and Add |
ユーザー |
![]() |
提出日時 | 2019-09-13 22:37:23 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 22 ms / 1,000 ms |
コード長 | 1,353 bytes |
コンパイル時間 | 845 ms |
コンパイル使用メモリ | 101,708 KB |
実行使用メモリ | 15,884 KB |
最終ジャッジ日時 | 2024-07-04 10:05:24 |
合計ジャッジ時間 | 1,516 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 9 |
ソースコード
#include <cstdio> #include <cstdlib> #include <cmath> #include <climits> #include <cassert> #include <cstdint> #include <iostream> #include <iomanip> #include <string> #include <stack> #include <queue> #include <vector> #include <map> #include <set> #include <algorithm> #include <numeric> #include <bitset> using namespace std; using ll = long long; using Pll = pair<ll, ll>; using Pii = pair<int, int>; constexpr ll MOD = 1000000007; constexpr long double EPS = 1e-10; constexpr int dyx[4][2] = { { 0, 1}, {-1, 0}, {0,-1}, {1, 0} }; int main() { std::ios::sync_with_stdio(false); cin.tie(nullptr); string s; cin >> s; int n = s.length(); vector<int> a(n+1, 0), ones(n+1, 0); for(int i=0;i<n;++i) a[i] = s[n-1-i] - '0'; ones[0] = a[0]; for(int i=1;i<n;++i) { ones[i] = ones[i-1] + a[i]; } ones[n] = ones[n-1]; vector<vector<int>> dp(n+1, vector<int>(2, 0)); dp[0][0] = a[0]; dp[0][1] = 1-a[0]; for(int i=1;i<=n;++i) { if(a[i]) { dp[i][0] = dp[i-1][0] + 1; dp[i][1] = min(dp[i-1][0], dp[i-1][1]); } else { dp[i][0] = min(dp[i-1][0], dp[i-1][1] + 2); dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + 1; } // cerr << i << ": " << dp[i][0] << ", " << dp[i][1] << endl; } cout << dp[n][0] << endl; }