Source Code
#include<bits/stdc++.h>
#define ll long long
#define tt "\n"
#define loop(i,n) for(int i = 0 ; i < n; i++)
#define pb push_back
#define in insert
#define ff first
#define ss second
#define FMP make_pair
#define all(v) v.begin(),v.end()
const ll MOD = 1000000000+7;
#define YN(a) a?cout << "YES\n":cout << "NO\n";
using namespace std;
void input()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
ll fastPowerMod(ll x, ll y, ll mod)
{
    if(y == 0)
        return 1 % mod;
    ll u = fastPowerMod(x, y / 2 ,mod);
    u = u * u % mod;
    if(y & 1)
        u = u * x % mod;
    return u;
}
ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
vector < vector < ll > > v(200005);
bool vis[200005];
ll dfs(ll root){
    vis[root] = 1;
    ll sum = 1;
    for(int i = 0 ; i < v[root].size(); i++){
        if(!vis[v[root][i]]){
            sum+=dfs(v[root][i]);
        }
    }
    return sum;
}
int main()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    cout.tie(NULL);
    input();
    ll n ;
    cin >> n;
    ll a[n];
    loop(i , n){
        cin >> a[i];
    }
    ll c = 0 , m = 0 , ans = 0;
    loop(i , n){
        if(a[i] == 1 and c > 0)
            continue;
        if(a[i] == 0 and m > 0)
            continue;
        if(a[i] == 1)
            c++;
        else
            m++;
        if(c > 0 and m > 0)
        {
            ans++;
            c = 0 ;
            m = 0;
        }
    }
    cout << ans << endl;
}



Copy
Tha Bits HashemGhanim
GNU G++17
11 ms
6.5 MB
Accepted