E WaterLand
Time Limit:1000MS Memory Limit:65535K
描述
Zayhero likes traveling very much. His current step is WaterLand. WaterLand is consisted by many rivers (numbered from 1 to n),
all these rivers are origin from the source – the sea. The water from the sea will fill up the rivers through some water supply
tunnels, each tunnel lead to exactly one river, and no two tunnels are lead to same river. Each tunnel is controlled by a single
switch, if the switch in on, the river will be filled with water, otherwise the river will dry up.
Now, Zayhero found that some of these rivers are dry up since the switches are set off. Zayhero love to challenge
himself, so he decide to set these switches on in order to fill ALL rivers from the 1st one to the nth one.
However, the switches is special, if Zayhero change the status of the ith switch, the switches from the (i+1)th to
nth will change their status. For example, there are 5 rivers in WaterLand, thus there are 5 tunnels and 5 switches,
their current status are: on, off, on, on, on.
The 1st switch will not be changed since it is already on;
The 2nd switch will be changed, after that, the 2nd to the 5th switches will be changed at the same time, and thus
the status will be: on, on, off, off, off;
After change the 3rd switch, the status will be: on, on, on, on, on, and all the rivers are filled with water now,
Zayhero change 2 switches totally.
Now, please calculate how many switches are needed to be changed?
输入格式
The 1st line of input contains only 1 integer T (T <= 30), indicate the number of test cases.
There are 2 lines of each test case.
The 1st line of each test case contains only 1 integer n (n <= 10 ^ 4), indicate the number of rivers in RiverLand.
The 2nd line of each test case contains n integers Si, if Si = 0, indicate the status the ith switch is off, if Si = 1,
indicate the status is on. It is guarantee that Si will always be 0 or 1.
输出格式
For each test case, print only 1 integer for each test case, indicate how many switches are needed to be changed by Zayhero.
Please refer to sample output for more details.
输入样例
3
5
1 0 1 1 1
5
1 0 0 1 1
5
0 1 0 1 0
输出样例
Case 1: 2
Case 2: 2
Case 3: 5
找首1后不连续字符串个数
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int n,i=0;
scanf("%d",&n);
while(i++<n){
int m,c=0;
scanf("%d",&m);
int last=-1;
while(m--){
int temp;
scanf("%d",&temp);
if(last==-1&&temp==1)continue;
if(last!=temp){
++c;
last=temp;
}
}
printf("Case %d: %d\n",i,c);
}
return 0;
}