Codeforces 1077B Disturbed People(思维题)

Codeforces 1077B 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 nn integer numbers a1,a2,…,ana1,a2,…,an, where ai=1ai=1 if in the ii-th flat the light is on and ai=0ai=0 otherwise.

Vova thinks that people in the ii-th flats are disturbed and cannot sleep if and only if 1<i<n1<i<n and ai−1=ai+1=1ai−1=ai+1=1 and ai=0ai=0.

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

Input
The first line of the input contains one integer nn (3≤n≤1003≤n≤100) — the number of flats in the house.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (ai∈{0,1}ai∈{0,1}), where aiai is the state of light in the ii-th flat.

Output
Print only one integer — the minimum number kk such that if people from exactly kk 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;

解题思路:

我们要保证没有人被打扰,就是不存在 101这种情况,然后我们从左向右扫,如果扫到0,我们再判断一下他的左右是否都是1 ,如果都是1 我们就必须关左右其中一个,关哪个呢?肯定是关右边的啦,因为左边的已经是扫过了的,右边的灯关了,还有可能让后面被打扰的人不被打扰,有一丢丢贪心的思想。。就这样扫一遍就出来了。。贼简单

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
int a[110];
int main()
{
	int ans=0;
	while(cin>>n)
	{
		ans=0;
		for(int i=1;i<=n;i++)
			cin>>a[i];
		for(int i=2;i<n;i++)
		{
			if(a[i]==0)
			{
				if(a[i-1]==1&&a[i+1]==1)
				{
					a[i+1]=0;
					ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值