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. LocalStack和Local对象实现栈的管理

    flask里面有两个重要的类Local和LocalStack 输入from flask import globals 左键+ctrl点globals进入源码,进去后找57行 flask只会实例化出这两 ...

  2. Java中是使用增强for的null问题

    在使用List和Map等集合时,我们经常会使用增强for来进行遍历.但是这里面会存在一些问题.比如当你进行数据库查询是,得到的返回结果是List集合时,如果没有查询到符合要求的数据时List集合时nu ...

  3. flask db操作

    from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) # app.config[' ...

  4. wildfly添加JNDI驱动配置

    wildfly-9.0.1.Final/modules目录下新建com文件夹cd com && mkdir mysql && cd mysql && m ...

  5. linux 释放系统内存命令

    1.sync 因为系统在操作的过程当中,会把你的操作到的文件资料先保存到buffer中去,因为怕你在操作的过程中因为断电等原因遗失数据,所以在你操作过程中会把文件资料先缓存.所以我们执行sync命令, ...

  6. Python中的 x+=x 与 x = x + x的区别

    对于Python中的可变数据类型(列表,字典)来说,+= 和 ..=..+..是不同的 加等是直接在变量的值上面进行操作,会修改了原来变量的值 先等后加会重新分配一个内存空间,不会再原有的变量值上面进 ...

  7. vue mixins是什么及应用

    mixins是什么? 官网对此的解释比较文绉绉,通俗的理解很简单,就是提供功能抽象 如A,B,C ...Z等很多个页面用到同一个功能,此时的做法就应该把该功能抽象出来,mixins就是干这个的 当然, ...

  8. 基于tornado---异步并发接口

    1.目的 由于有多个程序和脚本需要对mysql进行读写数据库,每次在脚本中进行数据库的连接.用cursor进行操作过于麻烦,因此希望可以有一个脚本开放接口,只需要传入sql语句,就可以返回结果回来.因 ...

  9. PAT甲级1003题解——Dijkstra

    解题步骤: 1.初始化:设置mat[][]存放点之间的距离,vis[]存放点的选取情况,people[]存放初始时每个城市的人数,man[]存放到达每个城市的救援队的最多的人数,num[]存放到达每个 ...

  10. java 的任意进制间转换

    直接上代码: public class Main { public static void main(String[] args) { // TODO Auto-generated method st ...