ZOJ 1622 Switch
2012-08-25 17:29
176 查看
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
题意:把序列转换成10101.....或010101...需要改变的最小数目
思路:此题主要思路是,2种方案都实现1次,比较哪个次数少。每次比较好后,n都要变化。
代码:
#include <stdio.h>
int main()
{
int N;
int a[10000];
while(scanf("%d",&N)!=EOF)
{
int i;
for(i=0;i<N;i++)
scanf("%d",&a[i]);
int n=1,r1=0,r2=0;
for(i=0;i<N;i++)
{
if(a[i]!=n)
r1++;
n=1-n;
}
n=0;
for(i=0;i<N;i++)
{
if(a[i]!=n)
r2++;
n=1-n;
}
printf("%d\n",r1<r2?r1:r2);
}
return 0;
}
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
题意:把序列转换成10101.....或010101...需要改变的最小数目
思路:此题主要思路是,2种方案都实现1次,比较哪个次数少。每次比较好后,n都要变化。
代码:
#include <stdio.h>
int main()
{
int N;
int a[10000];
while(scanf("%d",&N)!=EOF)
{
int i;
for(i=0;i<N;i++)
scanf("%d",&a[i]);
int n=1,r1=0,r2=0;
for(i=0;i<N;i++)
{
if(a[i]!=n)
r1++;
n=1-n;
}
n=0;
for(i=0;i<N;i++)
{
if(a[i]!=n)
r2++;
n=1-n;
}
printf("%d\n",r1<r2?r1:r2);
}
return 0;
}
相关文章推荐
- (水水水)zoj 1622 switch
- zoj 1622 Switch 开关灯 简单枚举
- zoj 1622 Switch 开关灯 简单枚举
- zoj 1622 switch
- ZOJ Problem Set–1622 Switch
- zoj1622----------------------------Switch
- zoj 1622 Switch
- zoj 1622 Switch
- ZOJ 1622 Switch
- ZOJ 1622
- ZOJ-1622
- 1622 Switch
- 1622_Switch
- zoj1622
- zoj 1622
- zoj 2332 Gems
- Adding Reversed Numbers(zoj 2001)
- ZOJ-1232 Adventure of Super Mario
- ZOJ 2526 FatMouse and JavaBean II 单源最短路 dijkstra
- zoj 2818 Root of the Problem