您的位置:首页 > 其它

Switch

2015-12-05 23:36 295 查看

Switch


Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^

题目描述

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.

输入

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

输出

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

示例输入

3 1 1 1
3 1 0 1


示例输出

1
0


提示

来源

示例程序

#include<iostream>

using namespace std;

int main()

{

int lights;

while(cin>>lights)

{

bool *p0 = new bool[lights], *p1 = new bool[lights];

bool *L = new bool[lights];

int switch1 = 0, switch2 = 0;

bool light;

for(int i = 0; i < lights; i++)

{

cin>>light;

if(i == 0)

{

*(p0 + i) = true;

*(p1 + i) = false;

}

else

{

*(p0 + i) = !*(p0 + i - 1);

*(p1 + i) = !*(p1 + i - 1);

}

if(light != *(p0 + i)) switch1++;

if(light != *(p1 + i)) switch2++;

}

cout<<(switch1 > switch2 ? switch2 : switch1)<<endl;

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: