题解:CF1244B Rooms and Staircases

题面

Rooms and Staircases

题面翻译

题目描述

有两层房间,每层 n n n 个,我们用数对 ( a , b ) (a, b) (a,b) 来表示每个房子,其中 a a a 表示第几层, b b b 表示从左向右数第几个

对于房子 ( 1 , i ) (1, i) (1,i) ( 2 , i ) (2, i) (2,i),都与 ( 1 , i − 1 ) , ( 1 , i + 1 ) (1, i - 1), (1, i + 1) (1,i1),(1,i+1) ( 2 , i − 1 ) , ( 2 , i + 1 ) (2, i - 1), (2, i + 1) (2,i1),(2,i+1) 相连

而在若干个或个位置中,又有一个双向的梯子,具体来说,若在 i i i 的位置有一个梯子,则 ( 1 , i ) , ( 2 , i ) (1, i), (2, i) (1,i),(2,i) 是相连的

求不重复经过同一个房间的情况下,最多能走过多少个房间

输入输出格式

输入格式

第一行给出测试数据组数

对于每组测试数据:

第一行给出 n n n,意义如上

第二行给出一个长度为 n n n 01 01 01 串,若在 i i i 位为 1 1 1,代表在位置 i i i 有个梯子

输出格式

不重复经过同一个房间的情况下,最多能走过多少个房间

题目描述

Nikolay lives in a two-storied house. There are n n n rooms on each floor, arranged in a row and numbered from one from left to right. So each room can be represented by the number of the floor and the number of the room on this floor (room number is an integer between 1 1 1 and n n n ).

If Nikolay is currently in some room, he can move to any of the neighbouring rooms (if they exist). Rooms with numbers i i i and i + 1 i+1 i+1 on each floor are neighbouring, for all 1 ≤ i ≤ n − 1 1 \leq i \leq n - 1 1in1 . There may also be staircases that connect two rooms from different floors having the same numbers. If there is a staircase connecting the room x x x on the first floor and the room x x x on the second floor, then Nikolay can use it to move from one room to another.


The picture illustrates a house with n = 4 n = 4 n=4 . There is a staircase between the room 2 2 2 on the first floor and the room 2 2 2 on the second floor, and another staircase between the room 4 4 4 on the first floor and the room 4 4 4 on the second floor. The arrows denote possible directions in which Nikolay can move. The picture corresponds to the string “0101” in the input. Nikolay wants to move through some rooms in his house. To do this, he firstly chooses any room where he starts. Then Nikolay moves between rooms according to the aforementioned rules. Nikolay never visits the same room twice (he won’t enter a room where he has already been).

Calculate the maximum number of rooms Nikolay can visit during his tour, if:

  • he can start in any room on any floor of his choice,
  • and he won’t visit the same room twice.

输入格式

The first line of the input contains one integer t ( 1 ≤ t ≤ 100 ) t ( 1 \le t \le 100 ) t(1t100)— the number of test cases in the input. Then test cases follow. Each test case consists of two lines.

The first line contains one integer n ( 1 ≤ n ≤ 1   000 ) n (1 \le n \le 1\,000) n(1n1000) — the number of rooms on each floor.

The second line contains one string consisting of n n n characters, each character is either a ‘0’ or a ‘1’. If the i i i -th character is a ‘1’, then there is a staircase between the room i i i on the first floor and the room i i i on the second floor. If the i i i -th character is a ‘0’, then there is no staircase between the room i i i on the first floor and the room i i i on the second floor.

In hacks it is allowed to use only one test case in the input, so t = 1 t = 1 t=1 should be satisfied.

输出格式

For each test case print one integer — the maximum number of rooms Nikolay can visit during his tour, if he can start in any room on any floor, and he won’t visit the same room twice.

样例 #1

样例输入 #1
4
5
00100
8
00000000
5
11111
3
110
样例输出 #1
6
8
10
6

我再放一个 CF 的测试点

样例 #2

样例输入 #2
14
1
0
1
1
2
00
2
01
2
10
2
11
3
000
3
001
3
010
3
011
3
100
3
101
3
110
3
111
样例输出 #2
1
2
2
4
4
4
3
6
4
6
6
6
6
6

提示

In the first test case Nikolay may start in the first room of the first floor. Then he moves to the second room on the first floor, and then — to the third room on the first floor. Then he uses a staircase to get to the third room on the second floor. Then he goes to the fourth room on the second floor, and then — to the fifth room on the second floor. So, Nikolay visits 6 6 6 rooms.

There are no staircases in the second test case, so Nikolay can only visit all rooms on the same floor (if he starts in the leftmost or in the rightmost room).

In the third test case it is possible to visit all rooms: first floor, first room → \rightarrow second floor, first room → \rightarrow second floor, second room → \rightarrow first floor, second room → \rightarrow first floor, third room → \rightarrow second floor, third room → \rightarrow second floor, fourth room → \rightarrow first floor, fourth room → \rightarrow first floor, fifth room → \rightarrow second floor, fifth room.

In the fourth test case it is also possible to visit all rooms: second floor, third room → \rightarrow second floor, second room → \rightarrow second floor, first room → \rightarrow first floor, first room → \rightarrow first floor, second room → \rightarrow first floor, third room.

题解

思路

先来整理一下可以走的位置

  • 对于一个房间都可以左右走

  • 对于一个有梯子的房间上下走

所以,这道题一共有 3 3 3 种情况。为了方便表达,我们约定:最左侧的楼梯位置为 l l l,最右侧的楼梯位置为 r r r,一共有 c n t cnt cnt 个楼梯。

  1. 反复横跳

    对于类似这种情况,最大走过房间的数量为 n + c n t n+cnt n+cnt

  2. 半路掉头

    对于类似这种情况,最大走过房间的数量为 2 × ( r + 1 ) 2 \times (r+1) 2×(r+1)

    或者

    对于类似这种情况,最大走过房间的数量为 2 × ( n − l ) 2 \times (n-l) 2×(nl)

    所以,两者综合起来就是 max ⁡ ( 2 × ( r + 1 ) , 2 × ( n − l ) ) \max(2 \times (r+1) , 2 \times (n-l)) max(2×(r+1),2×(nl))

  3. 无路可选

    这种情况只在 cnt == 0 的情况下为最大。而在 cnt != 0 时小于第 1 种情况。

    对于类似这种情况,最大走过房间的数量为 n n n

所以,最终的答案就是以上 3 3 3 中情况的最大值。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int T, n;
string s;
signed main() {
	cin.tie(0), cout.tie(0);
	cin >> T;
	while (T--) {
		cin >> n >> s;
		int l, r, cnt = 0;
		for (int i = 0; i < n; i++) //寻找 l 
			if (s[i] == '1') {
				l = i;
				break;
			}
		for (int i = n - 1; i >= 0; i--) // 寻找 r
			if (s[i] == '1') {
				r = i;
				break;
			}
		for (int i = 0; i < n; i++) // 楼梯的数量
			if (s[i] == '1')
				cnt++;
		if (cnt == 0) { //没有楼梯,最大为 n
			cout << n << "\n";
			continue;
		}
		cout << max(n + cnt, max(r * 2 + 2, (n - l) * 2)) << "\n";
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值