1. 程式人生 > >CodeForces1077B Disturbed People(思維)

CodeForces1077B Disturbed People(思維)

CodeForces1077B Disturbed People

Description

There is a house with n flats situated on the main street of Berlatov. Vova is watching this house every night. The house can be represented as an array of n integer numbers a1,a2,…,an, where ai=1 if in the i-th flat the light is on and ai=0 otherwise.Vova thinks that people in the i-th flats are disturbed and cannot sleep if and only if 1<i<n and ai−1=ai+1=1 and ai=0.

Vova is concerned by the following question: what is the minimum number k such that if people from exactly k pairwise distinct flats will turn off the lights then nobody will be disturbed? Your task is to find this number k.

Input

The first line of the input contains one integer n (3≤n≤100) — the number of flats in the house.The second line of the input contains n integers a1,a2,…,an(ai∈{0,1}), where ai is the state of light in the i-th flat.

Output

Print only one integer — the minimum number k such that if people from exactly k pairwise distinct flats will turn off the light then nobody will be disturbed.

Examples

Input

10
1 1 0 1 1 0 1 0 1 0

Output

2

Input

5
1 1 0 0 0

Output

0

Input

4
1 1 1 1

Output

0

Note

In the first example people from flats 2 and 7 or 4 and 7 can turn off the light and nobody will be disturbed. It can be shown that there is no better answer in this example.

There are no disturbed people in second and third examples.

題解

題意

當第i層為0且i-1層和i+1層都為1時,稱i被打擾,現在有一個操作可以使1變為0,問最少需要使多少1變為0使得不存在被打擾的樓層

思路

比較特殊的情況為10101,1010101。

對於10101,修改中間的1即可保證不會被打擾,而對於1010101這種情況,中間的兩個1都需要修改,因此記錄這些被打擾的位置,其中差值若為2,即可通過修改中間的1來完成需求。然而不能連續兩個都是相同位置,否則數目需要+1。

程式碼

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

const int MAXN = 100+10;
int a[MAXN];
int dis[MAXN];

void init(){
    memset(a,0,sizeof(a));
    memset(dis,INF,sizeof(dis));
}

int main(){
    int N;
    while(~scanf("%d",&N)){
        init();
        int tot = 0;
        for(int i=1;i<=N;i++){
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=N-1;i++){
            if(a[i-1]==1&&a[i+1]==1&&a[i]==0){
                dis[tot++] = i;
            }
        }
        int ans  =0;
        int flag= 0;
        for(int i=0;i<tot;i++){
            if(dis[i+1]-dis[i]<=2&&flag ==0)    {
                ans--;
                ans ++;
                flag = 1;
                continue;
            }
            flag = 0;
            ans++;
        }
        printf("%d\n",ans);
    }

    return 0;
}