結果

問題 No.3114 0→1
ユーザー AK_Mi
提出日時 2025-04-20 14:19:46
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,343 bytes
コンパイル時間 2,093 ms
コンパイル使用メモリ 193,388 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-04-20 14:19:50
合計ジャッジ時間 3,214 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(void){
    int n;
    int now = 0;
    int one = 0;
    int before = 1;
    int count = 0;
    string s;
    
    cin >> n >> s;
    
    for(int i = 0; i < n; i++){
        if(s.at(i) == '0'){
            now++;
        }
        
        if(s.at(i) == '1' || i == n - 1){
            //cout << now << " " << count << endl;
            if(i == 0){
                one++;
                continue;
            }
            if(s.at(i - 1) == '0' || i == n - 1){
                if(now % 3 == 0){
                    count += int(now / 3)*2;
                    before = 1;
                }else if(now % 3 == 1){
                    if(before == 1 || one > 1){
                        count += int(now / 3)*2;
                        before = 0;
                    }else{
                        count += int(now / 3)*2 + 1;
                        before = 1;                        
                    }
                }else if(now % 3 == 2){
                    if(before == 1 || one > 1)before = 1;
                    else before = 0;
                    count += int(now / 3)*2 + 1;
                }
                
                now = 0;
                one = 0;
            }
            
            one++;
        }
    }

    cout << count << endl;
    
    return 0;
}
0