题目链接 : 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,…,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
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 }