A12371 | Sushi for Two
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $t_i$ , where $t_i = 1$ means it is with tuna, and $t_i = 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.
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 $t_i$ , where $t_i = 1$ means it is with tuna, and $t_i = 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.
输入格式
The first line contains a single integer $n$ ( $2 \le n \le 100\,000$ ) — the number of pieces of sushi.
The second line contains $n$ integers $t_1$ , $t_2$ , ..., $t_n$ ( $t_i = 1$ , denoting a sushi with tuna or $t_i = 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.
The second line contains $n$ integers $t_1$ , $t_2$ , ..., $t_n$ ( $t_i = 1$ , denoting a sushi with tuna or $t_i = 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.
输出格式
Print a single integer — the maximum length of a valid continuous segment.
输入输出样例
输入 #1
7 2 2 2 1 1 2 2
输出 #1
4
输入 #2
6 1 2 1 2 1 2
输出 #2
2
输入 #3
9 2 2 1 1 1 2 2 2 2
输出 #3
6
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]$ .
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]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted