zoj 1622 Switch 开关灯 简单枚举
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
这道题很简单,我觉得之所以写是因为提供了自己的思路,在这里将奇数调为开,偶数调为关,
和为num1,这样可以得到另一面就是N-num1就是奇数调为关,偶数调为开,这样哪个结果小于N/2哪个就能成为最小的切换数
#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
int a[10005];
int main()
{
int i,n,tempo,tempj,temp;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
tempo=tempj=temp=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(i%2==0)
{
if(a[i]==1)
tempj++;
else
tempo++;
}
else
{
if(a[i]==0)
tempj++;
else
tempo++;
}
}
if(tempj>tempo)
temp=tempo;
else
temp=tempj;
printf("%d\n",temp);
}
return 0;
}
zoj 1622 Switch 开关灯 简单枚举的更多相关文章
- UVA - 10167 - Birthday Cake (简单枚举)
思路:简单枚举 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include &l ...
- Java练习 SDUT-1959_简单枚举类型——植物与颜色
简单枚举类型--植物与颜色 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 请定义具有red, orange, yell ...
- hdu5073 简单枚举+精度处理
其实这题还是挺简单的,因为移动k个星球后,这k个星球的权值就可以变为0,所以只有剩下的本来就是连着的才是最优解,也就是说要动也是动两端的,那么就O(N)枚举一遍动哪些就好了. 我是在杭电oj题目重现的 ...
- UVa 725 简单枚举+整数转换为字符串
Division Write a program that finds and displays all pairs of 5-digit numbers that between them use ...
- ZOJ 3987 Numbers(Java枚举)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3987 题意:给出一个数n,现在要将它分为m个数,这m个数相加起来必须等于n ...
- zoj 3983 Crusaders Quest 思维+枚举
题目链接 这道题意思是: 给你一个长度为9的字符串,且只有3个a.3个g.3个o 问,你可以选择删除一段连续的串或者单个的字符也可以不删,最多会出现几个三子相连的子串 比如:agoagoago只有将两 ...
- ZOJ 3213 Beautiful Meadow 简单路径 插头DP
简单路径的题目,其实就是在状态后面多记了有多少个独立插头. 分类讨论独立插头: 1.只存在上插头或者左插头,可以选择作为独立插头. 2.都不存在上插头和左插头,选择作为独立插头的同时要标号为新的连通块 ...
- UVA 725 UVA 10976 简单枚举
UVA 725 题意:0~9十个数组成两个5位数(或0开头的四位数),要求两数之商等于输入的数据n.abcde/fghij=n. 思路:暴力枚举,枚举fghij的情况算出abcde判断是否符合题目条件 ...
- 使用if和switch制作简单的年龄生肖判断
-年 查询 --> var oDiv =document.getElementById("cont"); var oYear = document.getElementByI ...
随机推荐
- javascript中实现sleep的两种方式
最近在js中要使用到类似于C++中的sleep函数(让cpu休眠).但是js是不可能让cpu休眠,所以可以通过下面的两种方式模拟sleep函数. 方式一:使用setTimeout函数代替.如果在一个循 ...
- win7 64bit下最新Apahe2.4.18+php7.0.2+MySQL5.7.10配置
原文:win7 64bit下最新Apahe2.4.18+php7.0.2+MySQL5.7.10配置 一.说明 以前配置apache+php+mysql都是参考网上的,一般都没有什么问题.最近公司有个 ...
- CSRF攻击[转]
一.CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSR ...
- nagios&pnp4nagios--yum 安装
一. 环境: 1. centos 6.4 2. 设置hostname 并且安装好apache 3. 关闭selinux及iptables 二. 安装nagios服务器端: 1. rpm -Uvh ht ...
- 在XML里的XSD和DTD以及standalone的使用
有关XML结构中的XSD和DTD以及standalone的使用 XmlDeclaration declare= document.CreateXmlDeclaration("1.0" ...
- Android开发之Service的写法以及与Activity的通信
Service的总结: 1.按运行地点分类: 类别 区别 优点 缺点 应用 本地服务(Local) 该服务依附在主进程上, 服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外 ...
- vijos1049送给圣诞夜的礼品
这题犯了两个sb错误,写下来,为以后做个警告 一.mul过程中将k作为了循环变量 二.看错了题…… 题目中说是数到k行,而我却以为数k遍…… 做矩阵乘法,只要记住一句话:置换一定可以写成矩阵的形式! ...
- I.MX6 android 禁止低电量自动关机
/************************************************************************ * I.MX6 android 禁止低电量自动关机 ...
- log4net 将日志写入数据库
asp.net利用log4net写入日志到SqlServer数据库,Log4net是一个开源的错误日志记录项目,易用性强,源自log4j,品质值得信赖. 下面就我的安装部署log4net到MS sql ...
- Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作 - Edison Chou
一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...