码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-12-21 12:08:12      阅读:71      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!