Sushi for Two CodeForces - 1138A 题解

博客详细分析了CodeForces上的一道编程题——Sushi for Two,探讨如何找到最长的连续且每种类型数量相等的寿司子序列。解释了题目的意思,给出了解题思路,即将连续数字视为块并存储每个块的数量,然后通过比较相邻块的大小来找出最大匹配长度。

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

Sushi for Two CodeForces - 1138A

题目

Arkady invited Anna for a dinner to a sushi restaurant. The restaurant is a bit unusual: it offers n pieces of sushi aligned in a row, and a customer has to choose a continuous subsegment of these sushi to buy.

The pieces of sushi are of two types: either with tuna or with eel. Let’s denote the type of the i-th from the left sushi as ti, where ti=1 means it is with tuna, and ti=2 means it is with eel.

Arkady does not like tuna, Anna does not like eel. Arkady wants to choose such a continuous subsegment of sushi that it has equal number of sushi of each type and each half of the subsegment has only sushi of one type. For example, subsegment [2,2,2,1,1,1] is valid, but subsegment [1,2,1,2,1,2] is not, because both halves contain both types of sushi.

Find the length of the longest continuous subsegment of sushi Arkady can buy.

Input
The first line contains a single integer n (2≤n≤100000) — the number of pieces of sushi.

The second line contains n integers t1, t2, …, tn (ti=1, denoting a sushi with tuna or ti=2, denoting a sushi with eel), representing the types of sushi from left to right.

It is guaranteed that there is at least one piece of sushi of each type. Note that it means that there is at least one valid continuous segment.

Output
Print a single integer — the maximum length of a valid continuous segment.

Examples
Input

7
2 2 2 1 1 2 2

Output

4


Input

6
1 2 1 2 1 2

Output

2


Input

9
2 2 1 1 1 2 2 2 2

Output

6

Note
In the first example Arkady can choose the subsegment [2,2,1,1] or the subsegment [1,1,2,2] with length 4.

In the second example there is no way but to choose one of the subsegments [2,1] or [1,2] with length 2.

In the third example Arkady’s best choice is the subsegment [1,1,1,2,2,2].

分析

题目大意

就是给出一个只有1和2的连续序列,然后选出最长连续的两种序列。如11222111的最长连续序列为222111,注意,求解的序列中2和1必须相连且数量一定要相等。

解题思路

将连续序列中不同的连续数字看成一块,并存储每块的数量。如1122211表示三块,则存储的值分别为2,3,2。
接下来只需要依次比较相邻块的个数,然后选出最大值,该值的两倍即为所求题解。注意依次比较时需要选出最小值作为该段匹配的个数,如11222111,第一块11和第二块222的匹配序列为2,即按较小的值取值。

代码

#include<cstdio>
#include<algorithm>
using namespace std;

int main(){
	int n = 0;
	int t[100010];
	int ans[100010]; \\存放每块的个数,如1122211表示三块,则对应ans值应分别为2,3,2
	
	scanf("%d",&n);
	for(int i = 0;i < n;i++){
		scanf("%d",&t[i]);
	}
	
	int sign = t[0]; \\标识,标示当前块的数字
	int count = 0; \\表示第几块
	for(int i = 0;i < n;i++){
		if(sign == t[i]){  
			ans[count]++;
		}else{ 数字不同,表示进入到下一块
			count++;
			ans[count] = 1;
			sign = t[i]; \\改变标识
		}
	
	}

	int Max = 0;
	for(int i = 1;i <= count;i++){
		int s = min(ans[i],ans[i-1]); \\与每个数与前一个数相比较,取其最小值,表示第i-1块和第i块相匹配的个数
		Max = max(Max,s); \\取最大值
	}
	printf("%d",Max*2);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值