頭良くなりたい人

文系大学生shadeのブログです。競技プログラミングや人文学の話題,受験ネタなど。

ABC124 C - Coloring Colorfully

問題はこちら atcoder.jp

方針

「どの隣り合う 2 枚のタイルも異なる色で塗」るということは,10101...01010...のいずれかです。

コード

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

int main(){
    string s;
    cin>>s;

    int count_1=0,count_2=0;
    //10101...
    for(int i=0; i<s.size(); i++){
        if(i%2==0){
            if(s[i]=='0'){
                count_1++;
            }
        }else{
            if(s[i]=='1'){
                count_1++;
            }
        }
    }
    //01010...
    for(int i=0; i<s.size(); i++){
        if(i%2==0){
            if(s[i]=='1'){
                count_2++;
            }
        }else{
            if(s[i]=='0'){
                count_2++;
            }
        }
    }

    cout<<min(count_1,count_2)<<endl;
}