Codeforces 1077b Disturbed People

题目链接 : http://codeforces.com/problemset/problem/1077/B   

             B. Disturbed People

There is a house with nn 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,,anwhere 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 1n and 

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 nn (3≤n≤100) — the number of flats in the house.

The second line of the input contains nn integers a1,a2,,an (ai{0,1}ai∈{0,1}), where aiai 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 22 and 77 or 44 and 77 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.

 

题意 : 像 1 0 1 这样的 ,两边都是1 中间0 , 0 会受到干扰,把需要的最小关灯数k求出来。

          这贪心题,我好像没用到贪心。

 

代码如下:

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 
 7 int n;                  // 房子的单位数
 8 int a[200];            //  表示开关灯的房屋数组
 9 int ans=0;            //  最小的改变数量(关灯),就能达到任何人都不被打扰
10 
11 void greedy(void) {
12     int h;
13     for(h = 0 ; h < n ; ) {
14 
15         //如果a[h]为0
16         if(a[h]==0) {
17             //检查前后是否为 1
18             if(a[h-1]==1&&a[h+1]==1&&(h+1)<n) {
19                 // 如果前后为1,检查后面1的后面是否为0 ( 1 0 1  0/1 )
20                 if(a[h+2]==0&&(h+2)<n) { //注意 h+2 不越界,a[200]一开始默认全为0
21                     a[h+1]=0;            // 如果后面1的后面为0 ( 1 0 1 0 ),把后面的1 变为0
22                     h=h+2;               // h +2 进行下一次循环
23                     ans++;               //改变数 +1
24                     continue;
25                 }
26                 // 如果是( 1 0 1 1)
27                 a[h-1]=0;          // 前后都行吧应该
28                 h=h+1;            //  h +1
29                 ans++;           //   改变数+1
30             }
31             // 前后有一个不为1
32             else {
33                 h++;  // 寻找下一个0
34             }
35 
36         }
37 
38         // a[h]不是0 , 继续找
39         else {
40 
41             h++;
42 
43         }
44     }
45 }
46 
47 int main() {
48     int i,j;
49     scanf("%d",&n);
50     for( i =  0 ; i < n ; i++) {
51         scanf("%d",&a[i]);
52     }
53 
54     greedy();            //进入函数
55 
56     printf("%d\n",ans);
57 
58     return 0;
59 }

 

转载于:https://www.cnblogs.com/aying/p/10060507.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值