結果

問題 No.884 Eat and Add
ユーザー amaridekinaiamaridekinai
提出日時 2019-09-13 22:38:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,436 bytes
コンパイル時間 1,755 ms
コンパイル使用メモリ 171,188 KB
実行使用メモリ 4,396 KB
最終ジャッジ日時 2023-09-17 14:52:09
合計ジャッジ時間 2,506 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int diff(string a, string b){
   //2進数表示で、aからbの引き算してから、結果に含まれる1の個数を調べる
  
  deque<char> q;

  int pa = (int)a.size()-1; int pb = (int)b.size()-1;
  int k = 0; //k == 1で、すでに借りがある状態。なければ0.
  for(int i = 0; i < pa-pb; i++){ b = "0" + b;}  //桁を揃える
  int p = pa;
  while( p >= 0){
    if( k == 0){
     if( a[p] == '0' && b[p] == '0'){ q.push_front('0');}
     if( a[p] == '0' && b[p] == '1'){ q.push_front('1'); k = 1;}
     if( a[p] == '1' && b[p] == '0'){ q.push_front('1');}
     if( a[p] == '1' && b[p] == '1'){ q.push_front('0');}
    }
    else{//借りを残している場合
     if( a[p] == '0' && b[p] == '0'){ q.push_front('1');}//まだ借りは残る
     if( a[p] == '0' && b[p] == '1'){ q.push_front('0');}
     if( a[p] == '1' && b[p] == '0'){ q.push_front('0'); k = 0;}
     if( a[p] == '1' && b[p] == '1'){ q.push_front('1');}
    }
    p--;
  }
   int cnt = 0;
    for(int i = 0; i < (int) q.size(); i++){ if(q[i] == '1'){ cnt++;}}
  
   
  return cnt;
}
  
int main(){
  string S; cin >> S;
  int N = (int) S.size();
  
  string M = "1"; string m = "1";
  for(int i = 0; i < N; i++){ M += "0";}
  for(int i = 0; i < N-1; i++){ m += "0";}
  
  int res1 = diff(M,S); int res2 = diff(S,m);

  cout << min(res1,res2)+1 << endl;
  
  
  return 0;}

 
0