Educational Codeforces Round 78 (Rated for Div. 2) C. Berry Jam

Posted ydddd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 78 (Rated for Div. 2) C. Berry Jam相关的知识,希望对你有一定的参考价值。

链接:

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的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27