zoukankan      html  css  js  c++  java
  • 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;
    
    }
  • 相关阅读:
    java基础英语---第二天
    树莓派的版本
    Linux系统下安装.deb文件
    在Raspberry上安装ROS
    树莓派文件权限的转换
    树莓派中Linux的相关命令
    raspberry连接ssh和vnc
    链表的建立及释放
    一些小细节问题
    关于构建二维动态内存(堆)及释放
  • 原文地址:https://www.cnblogs.com/malloc/p/2429135.html
Copyright © 2011-2022 走看看