CF1278C.Berry Jam
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were 2n jars of strawberry and blueberry jam.
All the 2n jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly n jars to his left and n jars to his right.
For example, the basement might look like this:

Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right.
Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same.
For example, this might be the result:
He has eaten 1 jar to his left and then 5 jars to his right. There remained exactly 3 full jars of both strawberry and blueberry jam.
Jars are numbered from 1 to 2n from left to right, so Karlsson initially stands between jars n and n+1.
What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left?
Your program should answer t independent test cases.
卡尔松最近在房屋地下室发现了大量浆果果酱罐头。具体来说,共有 2n 罐草莓果酱和蓝莓果酱。
全部 2n 个罐头排成一列。通往地下室的楼梯恰好位于该列正中间。因此,当卡尔松进入地下室时,他左侧恰好看到 n 个罐头,右侧也恰好看到 n 个罐头。
例如,地下室可能如下图所示:

作为一个直率的人,他立刻开始吃果酱。每分钟,他选择清空其左侧第一个非空罐头,或清空其右侧第一个非空罐头。
最终,卡尔松决定:剩余的满罐草莓果酱与满罐蓝莓果酱数量必须相等。
例如,这可能是最终结果:

他先向左吃了 1 个罐头,再向右吃了 5 个罐头。最终恰好剩下 3 个满罐草莓果酱和 3 个满罐蓝莓果酱。
罐头从左到右编号为 1 至 2n,因此卡尔松初始位置恰在第 n 个与第 n+1 个罐头之间。
问:卡尔松至少需要清空多少个罐头,才能使剩余的满罐草莓果酱与满罐蓝莓果酱数量相等?
你的程序需处理 t 个相互独立的测试用例。
输入格式
The first line contains one integer t (1≤t≤1000) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤105).
The second line of each test case contains 2n integers a1,a2,…,a2n (1≤ai≤2) — ai=1 means that the i-th jar from the left is a strawberry jam jar and ai=2 means that it is a blueberry jam jar.
It is guaranteed that the sum of n over all test cases does not exceed 105.
第一行包含一个整数 t(1≤t≤1000)—— 表示测试用例的数量。
每个测试用例的第一行包含一个整数 n(1≤n≤105)。
每个测试用例的第二行包含 2n 个整数 a1,a2,…,a2n(1≤ai≤2)—— 其中 ai=1 表示从左往右数第 i 个罐子装的是草莓果酱,ai=2 表示装的是蓝莓果酱。
保证所有测试用例的 n 值之和不超过 105。
输出格式
For each test case print the answer to it — the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left.
对于每个测试用例,输出对应的答案——即卡尔森需要清空的最少罐子数量,使得剩余的草莓果酱罐头与蓝莓果酱罐头数量相等。
输入输出样例
输入#1
4 6 1 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 3 1 1 1 1 1 1 2 2 1 1 1
输出#1
6 0 6 2
说明/提示
The picture from the statement describes the first test case.
In the second test case the number of strawberry and blueberry jam jars is already equal.
In the third test case Karlsson is required to eat all 6 jars so that there remain 0 jars of both jams.
In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave 1 jar of both jams.
题目附图描述了第一个测试用例。
在第二个测试用例中,草莓果酱罐头与蓝莓果酱罐头的数量已经相等。
在第三个测试用例中,卡尔松需要吃掉全部 6 个罐头,使得两种果酱剩余的罐头数均为 0。
在第四个测试用例中,卡尔松可以清空第二个和第三个罐头,或者清空第三个和第四个罐头。这两种方案均会使两种果酱各剩余 1 个罐头。
输入解题思路,AI测评打分。不知道怎么写?