UVALive 3468 Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t, i, j, n, d = 0, g;
    while(cin >> n && n > 0)
    {
        int a[n];
        for(i=0; i<n; i++)
        {
            cin >> a[i];
        }
        int c = 0, f = 0;
        t = 0, d = 0, g = 0;
        for(i=0; i < n; i++)
        {
            if(a[i] == 0)
                t++;
            else
                break;
            if(i == n-1)
                d = 1;
        }
        if(d != 1)
        {
            for(i=n-1; i>=0; i--)
            {
                if(a[i] == 0)
                    g++;
                else
                    break;
            }
        }
        if(t!= 0 && g!= 0 && t%2 == 1 && g%2 == 1&& d != 1)
            c += 1;
        d = 0;
        for(i=0; i<n; i++)
        {
            if(a[i] == 0)
            {
                f++;
            }
            else
            {
                int p = f/2;
                c += p;
                f = 0;
                d  = 1;
            }
        }
        if(f > 1)
        {
            if(d != 1 && f%2 == 1)
                c +=1;
            c += f/2;
        }
        cout << c << endl;
    }

    return 0;
}

Comments