ZOJ Problem Set–1622 Switch

Time Limit: 2 Seconds      Memory Limit: 65536 KB


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;
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;
}
posted @ 2012-04-01 17:37  Gavin Lipeng Ma  阅读(275)  评论(0编辑  收藏  举报