CUC-SUMMER-4-D

D - Switch
ZOJ - 1622

There are N lights in a line. Given the states (on/off) of the lights, your task is to determine at least how many lights should be switched (from on to off, or from off to on), in order to make the lights on and off alternatively.

Input

One line for each testcase.

The integer N (1 <= N <= 10000) comes first and is followed by N integers representing the states of the lights ("1" for on and "0" for off).

Process to the end-of-file.

Output

For each testcase output a line consists of only the least times of switches.

Sample Input

3 1 1 1
3 1 0 1

Sample Output

1
0


题意:灯开关分别用0 1表示,求最少开或关几盏灯使灯成为01交替的序列。

解法:水题,两种情况,0开始或1开始,一趟遍历,分别用x,y记录每种情况要动几盏灯,最后取xy中最小值。

代码:

#include<iostream>
using namespace std;
int a[10005];
int main()
{
    int num;
    while(cin>>num){
        for(int i=0;i<num;i++)
            cin>>a[i];
        int x=0,y=0;
        for(int i=0;i<num;i++)
            i%2?(a[i]?x++:y++):(!a[i]?x++:y++);
        cout<<min(x,y)<<endl;
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容