标签:recently problem required else class either you ber ref
https://codeforces.com/contest/1278/problem/C
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.
记录左边,枚举右边。。。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 2e5+10;
int a[MAXN];
map<int, int> Mp;
int n;
int main()
{
int t;
cin >> t;
while(t--)
{
Mp.clear();
cin >> n;
for (int i = 1;i <= 2*n;i++)
cin >> a[i];
int sum = 0;
Mp[0] = 0;
for (int i = 1;i <= n;i++)
{
if (a[i] == 1)
sum++;
else
sum--;
Mp[sum] = i;
}
sum = 0;
int ans = 2*n;
ans = min(ans, 2*n-Mp[0]);
for (int i = 2*n;i >= n+1;i--)
{
if (a[i] == 1)
sum++;
else
sum--;
if (sum == 0)
ans = min(ans, i-Mp[0]-1);
if (Mp[0-sum] != 0)
ans = min(ans, i-Mp[0-sum]-1);
}
cout << ans << endl;
}
return 0;
}
Educational Codeforces Round 78 (Rated for Div. 2) C. Berry Jam
标签:recently problem required else class either you ber ref
原文地址:https://www.cnblogs.com/YDDDD/p/12076352.html