結果

問題 No.884 Eat and Add
ユーザー amaridekinaiamaridekinai
提出日時 2019-09-14 05:13:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 1,000 ms
コード長 851 bytes
コンパイル時間 1,431 ms
コンパイル使用メモリ 165,408 KB
実行使用メモリ 6,728 KB
最終ジャッジ日時 2023-09-18 09:38:53
合計ジャッジ時間 2,133 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,468 KB
testcase_01 AC 3 ms
6,436 KB
testcase_02 AC 3 ms
6,416 KB
testcase_03 AC 9 ms
6,728 KB
testcase_04 AC 8 ms
6,672 KB
testcase_05 AC 7 ms
6,548 KB
testcase_06 AC 8 ms
6,524 KB
testcase_07 AC 7 ms
6,544 KB
testcase_08 AC 7 ms
6,632 KB
testcase_09 AC 8 ms
6,612 KB
testcase_10 AC 3 ms
6,548 KB
testcase_11 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;

void chmin(ll &a, ll b){
  if( a > b){ swap(a,b); } return;}

int main(){
string N; cin >> N;
  ll m = (ll)N.size();
  if( m == 1){ cout << 1 << endl; return 0;}
  
  ll dp[200100][2];
  for(ll i = 0; i < 200100; i++){
    for(ll j = 0; j < 2; j++){ dp[i][j] = INF;}}
  reverse(N.begin(),N.end());
  
   if( N[0] == '0'){
     dp[1][0] = 0LL ;}
  else{
     dp[1][1] = 1LL; dp[1][0] = 1LL;}
       
  
  for(ll i = 1; i < m ; i++){
    if( N[i] == '0'){
      chmin(dp[i+1][0],dp[i][0]);
      chmin(dp[i+1][1], dp[i][1]+1);
      chmin(dp[i+1][0], dp[i][1]+1);}
    else{
      chmin(dp[i+1][1],dp[i][0]+1);
      chmin(dp[i+1][0],dp[i][0]+1);
      chmin(dp[i+1][1],dp[i][1]);}
  }
 
  cout << min(dp[m][0],dp[m][1]+1LL) << endl;
  
  return 0;}
      
  
0