[Codeforces1244B] Rooms and Staircases

上下对称,等效的有效起点只有最左的和最右的两个点

没有1时答案显然是n

1个1答案是n+1

多个1呢?

一共考虑3种情况

1.从最左到最右边(2表示陆径) 

0010101

 

2220222

0022202

有1一定会走,这会让答案+1,没有副作用。

2.从左到左

0010101

 

2222222

2222222

显然在最右边1处走下来比较好。

3从右到右

0010101

 

0022222

0022222

同样,显然在最左边1处走下来比较好。

#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
 
using namespace std;

typedef long long LL;

int f[1010][3],n,flag = 0;
char s[1010];
int st[1010],tp;
  
int main() {
	int T;
	cin >> T;
	while(T--) {
		scanf("%d%s",&n,s+1);
		//for (int i = 1; i <= n; i++)
		  //f[i][0] = f[i][1] = f[i][2] = 1;
		int ans = 0; tp = 0;
		st[0] = 0;
		for (int i = 1; i <= n; i++) {
		  //  flag++;
			//ans = max(ans,dfs(i));
		   if (s[i] == '1') st[++tp] = i;
		}
		if (tp == 0) ans = n;
		else if (tp == 1) ans = max(n+1,max(st[1]*2,(n-st[1]+1)*2));
		else ans = max(n+tp,max(st[tp]*2,(n-st[1]+1)*2));
		printf("%d\n",ans);
	}
    return 0;
}

Nikolay lives in a two-storied house. There are nn 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 11 and nn).

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

The picture illustrates a house with n=4n=4. There is a staircase between the room 22 on the first floor and the room 22 on the second floor, and another staircase between the room 44 on the first floor and the room 44 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.

Input

The first line of the input contains one integer tt (1≤t≤1001≤t≤100) — 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 nn (1≤n≤1000)(1≤n≤1000) — the number of rooms on each floor.

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

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

Output

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.

Example

input

Copy

4
5
00100
8
00000000
5
11111
3
110

output

Copy

6
8
10
6

Note

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 66 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 →→ second floor, first room →→ second floor, second room →→ first floor, second room →→ first floor, third room →→ second floor, third room →→ second floor, fourth room →→ first floor, fourth room →→ first floor, fifth room →→ second floor, fifth room.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值