zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
  • 相关阅读:
    浅析Json底层
    浅析servlet
    xml树结构
    JS中BOM和DOM之间的关系
    Test openLiveWriter
    JAVA静态代理动态代理详解
    java多态实现示例
    java类中的变量以及方法的执行顺序
    java值传递
    将javaweb项目部署到服务器中
  • 原文地址:https://www.cnblogs.com/caomingpei/p/9974580.html
Copyright © 2011-2022 走看看