Educational Codeforces Round 78 C - Berry Jam

Educational Codeforces Round 78 C - Berry Jam

题目

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≤105).

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 105.

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.

题意

可以从中间往左连续吃几罐,从中间往右连续吃几罐,最少一共吃多少可以使剩下的红蓝罐数目相等


思路

这个题需要换成1-1序列之后,求出一个前缀和与后缀和相加等于0的位置,后缀和可以由前缀和相减得到,所以我们只计算一个前缀和;枚举右边吃了多少,对于每一个后缀和查找是否有和他互为相反数的前缀和(用一个map来记录前一半数的前缀和等于某个值时出现的位置,后缀和用前缀和相减就可以了。然后从中间向后遍历,遍历的值减去最后前缀和的值,然后看看map里面是否有这个值,如果有计算一下需要吃几个,每次取最小的结果就可以了),最后就是注意边界问题,因为可能往左往右一个不吃;

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int sum[maxn*2];
void solve(){
    int n;
    cin >> n;
    map<int, int> pos;
    pos[0] = 0; //往左全部吃完
    memset(sum, 0, sizeof(sum));
    for(int i=1; i<=n*2; i++){
        int a;
        cin >> a;
        if(a == 2)
            a = -1;     
        sum[i] = sum[i-1] + a;
        if(i <= n)
            pos[sum[i]] = i;
    }
    int ans = n*2;
    for(int i=n; i<=n*2; i++){
        auto it = pos.find(sum[i]-sum[2*n]);
        if(it != pos.end()){
            ans = min(i-it->second, ans);
        }
    }
    cout << ans << endl;
}
int main(){
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

​ 我们用sum[i]-sum[n*2],是因为前缀合计算的是 1比2多了几个,我们这样减了一下刚好是2比1多了几个。如果前面刚好有1比2多的数量等于后面从i这个位置2比1多的数量,那么把中间的这部分吃了就刚好。我们这样顺序遍历下最后取最小值就可以了。

全部评论

相关推荐

酷酷的喜马拉雅山:感觉这比一直在初筛不动的好多了
点赞 评论 收藏
分享
10-16 15:48
算法工程师
点赞 评论 收藏
分享
头像
11-03 16:48
已编辑
百度_高级研发工程师
事实是检验真理的唯一标准。&nbsp;无论我们怎么去说,去讲述,去证明,都抵不过一个offer来得实在,无论我们怎么去复现求职中的摸爬滚打、扒皮抽筋、狼狈不堪,都抵不过你在简历写上大厂的名字(外包不算)。&nbsp;所以在我求职期间,我什么话都不说,什么话都不讲,因为没有意义,虽然我总讲过程才是意义,但只有当你上岸的那一刻,你才有资格回想在水里的挣扎,只有等你出了山,你才知道山的全貌。&nbsp;我为什么一定要离开华为OD,难道它不稳定吗,不能赚钱吗。为了证明自己,那肯定有的。其实更多的是印证我的认知是否真的正确。&nbsp;(给不了解我的人交代一下背景,在下双非一本,gap一年,华为OD外包,摸爬滚打4个月,艰难上岸百度正编)一、...
先锋战士:说得很真诚。鄙视链自古有之,学历,家庭背景,财富,权利。从小有之,小学羡慕那些当班委的,中学羡慕那些学生会的,高中羡慕尖子班拿教学金的,大学羡慕高绩点,毕业了羡慕进大厂的。工作了,又羡慕高职级的,再后来又羡慕别人早早结婚的。我想表达的观点很简单,无论是华为od还是百度,都是经历,没有孰高孰低,为了抵达下一个风景,总会付出更多东西,但不就是人生吗?正如登山,每个阶段的山,都要想办法攀登,在博主的文字中,见到了坚持和积极寻找问题解决办法的心态
学历对求职的影响
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务