zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    使用MacPorts配置PHP开发环境(PHP54+PHP FPM+NGINX+MYSQL55)
    freebsd make 常用命令(非原创)
    可以通过以下步骤生成一个简单的证书:
    Javascript相关的一些碎裂的记忆
    中兴EBG2100路由器固件
    一些javascript内容
    freebsd 记事之PHP环境搭建
    vue3 中使用 vite 时的报错
    Vite2.0 按需引入Element Plus
    移动端横屏
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12076352.html
Copyright © 2011-2022 走看看