1622_Switch

来源:互联网 发布:cnc编程用什么软件好 编辑:程序博客网 时间:2024/06/11 08:11
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



Author: SHI, Xiaohan

Source: Zhejiang University 2003 Summer Camp Qualification Contest

××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××

#include<iostream>
using namespace std;
#define max 10000
int main()
{
int num;
bool lig[max]={0};
int i;
int on_first,off_first;
while(cin>>num)//往简单的想,无非就是两种情况,首位为1或0;分别求出需要的次数比较
{
on_first=off_first=0;
for(i=0;i!=num;i++)
cin>>lig[i];
for(i=0;i!=num/2;i++)
{
if(lig[2*i]==1)//这两个判断得到010101需要换多少
++off_first;
if(lig[2*i+1]==0)
++off_first;
if(lig[2*i]==0)//这两个判断得到101010需要换多少
++on_first;
if(lig[2*i+1]==1)
++on_first;


}
if(num%2==1)
{
if(lig[num-1]==1)
++off_first;
if(lig[num-1]==0)
++on_first;
}
//cout<<off_first<<" "<<on_first<<endl;
cout<<(off_first<on_first?off_first:on_first)<<endl;




}
return 0;
}

原创粉丝点击