Wednesday, 23 September 2015

Tornado! (UVALive 3468, POJ 2862, Regionals 2005 >> Latin America - South America)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 5005


int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n, arr[MX];
    string s;
    char ch;
    while(cin>>n && n)
    {
        for(int i=0;i<n;i++) cin>>arr[i];

        int idx=-1;
        for(int i=0;i<n;i++)
            if(arr[i])
            {
                idx=i;
                break;
            }
        if(idx==-1)
        {
            cout<<(n+1)/2<<"\n";
            continue;
        }
        vector<int>v; //to make both end with 1
        for(int i=idx;i<n;i++)  v.push_back(arr[i]);
        for(int i=0;i<idx;i++)  v.push_back(arr[i]);
        v.push_back(arr[idx]);

        int cnt=0, tot=0;
        for(int i=1;i<n;i++)
        {
            while(!v[i])
            {
                cnt++;
                i++;
            }
            tot+=cnt/2;
            cnt=0;
        }

        cout<< tot <<"\n";
    }
return 0;
}

No comments:

Post a Comment