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;
}
 

思路:

很好的一道思維題

這種問題容易想到雙指針這種算法上去,但是具體問題還要從具體問題分析入手。

這種計數的問題,一半用前綴和或者差分處理的思路通常是沒有問題的。

首先,從左到右計算梯子左側的藍莓醬和草莓醬數量的差,每次記錄當前差值對應的最大位置。

然後從左至右計算梯子右側草莓醬和藍莓醬之間的差值,如果發現右側的某個差值正好是左側某個差值的相反數,那麼就找到了一個左側和右側的對應位置,分別把梯子到這兩側對應位置的醬都吃乾淨就是最優答案。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章