SCAUOJ——2019年校赛热身-WaterLand

本文介绍了一个有趣的编程问题:如何计算最少需要改变多少个开关状态,才能确保所有河流都被海水填满。通过遍历输入的开关状态序列并使用特定规则进行处理,文章提供了一种有效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                               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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值