cf Educational Codeforces Round 78 C. Berry Jam

原题:
C. Berry Jam
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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≤10^5).

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 10^5.

Output

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.

Example
Input

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

Output

6
0
6
2

Note

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.

中文:

你家地窖里面有n罐草莓酱和n罐蓝莓酱,分别放在梯子的两侧。有一天你饿了,想吃酱,你想把酱吃成剩余的草莓酱和蓝莓酱的数量相同,问你最少吃多少?

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200002;
int a[maxn],bac[maxn/2];
int n,t;
unordered_map<int, int> uii;
int main()
{
	ios::sync_with_stdio(false);
	cin >> t;
	while (t--)
	{
		cin >> n;
		for (int i = 1; i <= n * 2; i++)
			cin >> a[i];
		uii.clear();
 
		int r = 0, b = 0;
 
		int ans = n * 2;
		for (int i = 1; i <= n; i++)
		{
			if (a[i] == 1)
				r++;
			else
				b++;
			if (r - b == 0)
				ans = min(ans, n + n - i);
			uii[r - b] = i;
		}
		memset(bac, 0, sizeof(bac));
		b = r = 0;
		for (int i = n *2; i >= n + 1; i--)
		{
			if (a[i] == 1)
				r++;
			else
				b++;
			if (b - r == 0)
			{
				ans = min(ans, i - 1);
			}
			if (uii.find(b - r) != uii.end())
			{
				ans = min(i - uii[b - r]-1, ans);
			}
		}
		cout << ans << endl;
	}
	return 0;
}
 

思路:

很好的一道思维题

这种问题容易想到双指针这种算法上去,但是具体问题还要从具体问题分析入手。

这种计数的问题,一半用前缀和或者差分处理的思路通常是没有问题的。

首先,从左到右计算梯子左侧的蓝莓酱和草莓酱数量的差,每次记录当前差值对应的最大位置。

然后从左至右计算梯子右侧草莓酱和蓝莓酱之间的差值,如果发现右侧的某个差值正好是左侧某个差值的相反数,那么就找到了一个左侧和右侧的对应位置,分别把梯子到这两侧对应位置的酱都吃干净就是最优答案。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值