• 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
    ",ans);
    	}
    
    	return 0;
    }
    
    
  • 相关阅读:
    6、深入理解计算机系统笔记:存储器层次结构,存储技术(1)
    流程图【占无内容】
    程序的三种基本控制架构【只有提纲】
    Console算法[for]穷举法:百钱买百鸡
    Logic算法(狼羊白菜)
    Console算法continue与break的区别?
    Console算法[for]简单画图
    Console算法[for]输出等腰三角形
    Console算法[for]国王与老人的六十四格
    Console算法[for]素数
  • 原文地址:https://www.cnblogs.com/caomingpei/p/9974580.html
Copyright © 2020-2023  润新知