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

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were 2n2n jars of strawberry and blueberry jam.

All the 2n2n 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 nn jars to his left and nn 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 11 jar to his left and then 55 jars to his right. There remained exactly 33 full jars of both strawberry and blueberry jam.

Jars are numbered from 11 to 2n2n from left to right, so Karlsson initially stands between jars nn and n+1n+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 tt independent test cases.

Input

The first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105).

The second line of each test case contains 2n2n integers a1,a2,…,a2na1,a2,…,a2n (1≤ai≤21≤ai≤2) — ai=1ai=1 means that the ii-th jar from the left is a strawberry jam jar and ai=2ai=2 means that it is a blueberry jam jar.

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

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.

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 66 jars so that there remain 00 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 11 jar of both jams.

题目就是要你查找【1~n】和【n+1~2n】两个区间的2和1相同的最小位置差,2是蓝莓,1是草莓,一个人站在n和n+1之间,开始向两边吃,问你怎么在吃的罐数最少情况下,2蓝莓和1草莓两个的总数量上相同。

我们可以把蓝莓2变成-1,1草莓任然为1变成1-1序列,求【1~n】前缀和 和 【n+1~2n】的后缀和

直接n*n暴力枚举:

n=10的5次方必然超时,所以我们不能这么暴力。

折半枚举:

我们可以看到,数组分成两个区间:【1~n】和【n+1~2n】。我们把前一半区间【1~n】的前缀和 和下标位置先保存起来,用map记录结果就不用考虑正负,用标记数组就要记得加上一个偏移量比如100000.

然后for循环一一枚举【n+1~2n】的区间判断是否有【1~n】前缀和 和 【n+1~2n】的后缀和相加为0的位置,取最小的位置差

需要注意的是:某位置后缀和其实可以通过2*n的后缀和-该位置的前缀和直接求出,所以我们不需要先求。

AC代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define M 200005
int inf=0x3f3f3f3f;
int sum[M];int c[M];
int main(void)
{
int t,n,a;
scanf("%d",&t);
while(t--)
{
//初始化
scanf("%d",&n);
for(int i=0;i<=200003;i++)
c[i]=inf;
sum[0]=0;
for(int i=1;i<=n*2;i++){
scanf("%d",&a);
if(a>1) a=-1;
sum[i]=sum[i-1]+a;//前缀和
if(i<=n)//记录前n的位置
c[sum[i]+100000]=i;
}
///折半枚举
if(c[0+100000]==inf)
c[0+100000]=0;
int minn=inf;
for(int j=n;j<=2*n;j++){
int h=sum[2*n]-sum[j];//j的后缀和(不包括j)
if(c[-h+100000]!=inf)//找和为0的位置
{
minn=min(minn,j-c[-h+100000]);
}
}
printf("%d\n",minn);
}
return 0;
}

  

codeforcesC - Berry Jam(折半枚举+1-1序列前后缀和)的更多相关文章

  1. CodeForces888E Maximum Subsequence(折半枚举+two-pointers)

    题意 给定一个包含\(n\)个数的序列\(a\),在其中任选若干个数,使得他们的和对\(m\)取模后最大.(\(n\leq 35\)) 题解 显然,\(2^n\)的暴枚是不现实的...,于是我们想到了 ...

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

  3. 折半枚举——poj3977

    暴力搜索超时,但是折半后两部分状态支持合并的情况,可用折半枚举算法 poj3977 给一个序列a[],从里面找到k个数,使其和的绝对值最小 经典折半枚举法+二分解决,对于前一半数开一个map,map[ ...

  4. Load Balancing 折半枚举大法好啊

    Load Balancing 给出每个学生的学分.   将学生按学分分成四组,使得sigma (sumi-n/4)最小.         算法:   折半枚举 #include <iostrea ...

  5. CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

    1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] ...

  6. NYOJ 1091 超大01背包(折半枚举)

    这道题乍一看是普通的01背包,最最基础的,但是仔细一看数据,发现普通的根本没法做,仔细观察数组发现n比较小,利用这个特点将它划分为前半部分和后半部分这样就好了,当时在网上找题解,找不到,后来在挑战程序 ...

  7. Codeforces 888E - Maximum Subsequence(折半枚举(meet-in-the-middle))

    888E - Maximum Subsequence 思路:折半枚举. 代码: #include<bits/stdc++.h> using namespace std; #define l ...

  8. Codeforces 912 E.Prime Gift (折半枚举、二分)

    题目链接:Prime Gift 题意: 给出了n(1<=n<=16)个互不相同的质数pi(2<=pi<=100),现在要求第k大个约数全在所给质数集的数.(保证这个数不超过1e ...

  9. poj_3977 折半枚举

    题目大意 给定N(N<=35)个数字,每个数字都<= 2^15. 其中一个或多个数字加和可以得到s,求出s的绝对值的最小值,并给出当s取绝对值最小值时,需要加和的数字的个数. 题目分析 需 ...

随机推荐

  1. LeetCode 5198. 丑数 III(Java)容斥原理和二分查找

    题目链接:5198. 丑数 III 请你帮忙设计一个程序,用来找出第 n 个丑数. 丑数是可以被 a 或 b 或 c 整除的 正整数. 示例 1: 输入:n = 3, a = 2, b = 3, c ...

  2. C#泛型集合之——哈希集合

    1.特点:HashSet 中元素不重复,容量为元素个数,自动增大.是一组值,是高性能的数学集合. 2.创建: (1)HashSet<类型> 集合名 = new HashSet<类型& ...

  3. C#使用Autofac实现控制反转IoC和面向切面编程AOP

    Autofac是一个.net下非常优秀,性能非常好的IOC容器(.net下效率最高的容器),加上AOP简直是如虎添翼.Autofac的AOP是通过Castle(也是一个容器)项目的核心部分实现的,名为 ...

  4. IIS err_connection_timed_out(响应时间过长)

    场景:我在服务器的IIS上部署了一个网站,服务器上可以正常打开,然后我用自己的电脑访问,出现如下错误: 原因:服务器的防火墙对入站规则进行了一些限制. 解决方法:1.打开服务器的防火墙-----> ...

  5. NMS的实现代码详解

    NMS代码说明(来自Fast-RCNN) 个人觉得NMS包含很多框,其坐标为(x1,y1,x2,y2),每个框对应了一个score,我们将按照score得分降序,并将第一个最高的score的框(我们叫 ...

  6. P2472 [SCOI2007]蜥蜴 (最大流)

    题目 P2472 [SCOI2007]蜥蜴 解析 这个题思路比较清晰,本(qi)来(shi)以(jiu)为(shi)无脑建图跑最大流,结果挂了,整了一个小时后重新建图才过的. 建立一个超级源点和一个超 ...

  7. 【转载】C#中使用int.Parse方法将字符串转换为整型Int类型

    在C#编程过程中,很多时候涉及到数据类型的转换,例如将字符串类型的变量转换为Int类型就是一个常见的类型转换操作,int.Parse方法是C#中专门用来将字符串转换为整型int的,int.Parse方 ...

  8. unity shader入门(一):基本结构话痨版

    unity shader 有三种形式:表面着色器(Surface Shader),顶点/片元着色器(Vertex/Fragment Shader),固定函数着色器(Fixed Function Sha ...

  9. [JavaScript] js中全局标识正则表达式的lastIndex属性

    在JavaScript中使用正则表达式时,遇到一个坑:第一次匹配是true,第二次匹配是false. 因为在带全局标识"g"的正则表达式对象中,才有“lastIndex” 属性,该 ...

  10. python系列:一、Urllib库的基本使用

    开篇介绍: 因为我本人也是初学者,爬虫的例子大部分都是学习资料上面来的,只是自己手敲了一遍,同时加上自己的理解. 写得不好请多谅解,如果有错误之处请多赐教. 我本人的开发环境是vscode,pytho ...