codeforcesC - Berry Jam(折半枚举+1-1序列前后缀和)
Educational Codeforces Round 78 (Rated for Div. 2) C - Berry Jam
2 seconds
256 megabytes
standard input
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.
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.
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
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序列前后缀和)的更多相关文章
- CodeForces888E Maximum Subsequence(折半枚举+two-pointers)
题意 给定一个包含\(n\)个数的序列\(a\),在其中任选若干个数,使得他们的和对\(m\)取模后最大.(\(n\leq 35\)) 题解 显然,\(2^n\)的暴枚是不现实的...,于是我们想到了 ...
- 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 ...
- 折半枚举——poj3977
暴力搜索超时,但是折半后两部分状态支持合并的情况,可用折半枚举算法 poj3977 给一个序列a[],从里面找到k个数,使其和的绝对值最小 经典折半枚举法+二分解决,对于前一半数开一个map,map[ ...
- Load Balancing 折半枚举大法好啊
Load Balancing 给出每个学生的学分. 将学生按学分分成四组,使得sigma (sumi-n/4)最小. 算法: 折半枚举 #include <iostrea ...
- CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。
1514: Packs Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 61 Solved: 4[Submit][Status][Web Board] ...
- NYOJ 1091 超大01背包(折半枚举)
这道题乍一看是普通的01背包,最最基础的,但是仔细一看数据,发现普通的根本没法做,仔细观察数组发现n比较小,利用这个特点将它划分为前半部分和后半部分这样就好了,当时在网上找题解,找不到,后来在挑战程序 ...
- Codeforces 888E - Maximum Subsequence(折半枚举(meet-in-the-middle))
888E - Maximum Subsequence 思路:折半枚举. 代码: #include<bits/stdc++.h> using namespace std; #define l ...
- Codeforces 912 E.Prime Gift (折半枚举、二分)
题目链接:Prime Gift 题意: 给出了n(1<=n<=16)个互不相同的质数pi(2<=pi<=100),现在要求第k大个约数全在所给质数集的数.(保证这个数不超过1e ...
- poj_3977 折半枚举
题目大意 给定N(N<=35)个数字,每个数字都<= 2^15. 其中一个或多个数字加和可以得到s,求出s的绝对值的最小值,并给出当s取绝对值最小值时,需要加和的数字的个数. 题目分析 需 ...
随机推荐
- PAT(B) 1030 完美数列 - C语言 - 滑动窗口 & 双指针
题目链接:1030 完美数列 (25 point(s)) 给定一个正整数数列,和正整数 \(p\),设这个数列中的最大值是 \(M\),最小值是 \(m\),如果 \(M≤mp\),则称这个数列是完美 ...
- redis源码分析(三)--rdb持久化
Redis rdb持久化 Redis支持两种持久化方式:rdb与aof.rdb将一个节点上的内存数据序列化后存储到磁盘中,序列化的数据以尽可能节约空间的方式存储,并非完全的ascii表示.它的优点在于 ...
- java知识精要(二)
java知识精要(一) 集合 Iterable v.s. Iterator 两者都是接口,在Collection继承的是Iterable. Iterable表达了集合具备迭代访问的能力,而Iterat ...
- jwt的思考
什么是jwt jwt的问题 jwt的是实践 https://www.pingidentity.com/en/company/blog/posts/2019/jwt-security-nobody-ta ...
- yii框架无限极分类的做法
用yii框架做了一个无限极分类,主要的数组转换都是粘贴的别人的代码,但还是不要脸的写出来,方便以后自己看 用的是递归,不是path路径 控制器: protected function subtree( ...
- java之mybatis之缓存
1.mybatis自带缓存功能.分为一级缓存,二级缓存. 2.一级缓存为 session 缓存,在一个 session中 ,一个查询的 select 语句只会执行一次,根据 <select&g ...
- 复杂dic的文件化存储和读取问题
今天遇到一个难题.整出一个复杂的dic,里面不仅维度多,还含有numpy.array.超级复杂.过程中希望能够存储一下,万一服务器停了呢?万一断电了呢? 结果存好存,取出来可就不是那样了.网上搜索了很 ...
- 在Centos6.5上部署kvm虚拟化技术
KVM是什么? KVM 全称是 基于内核的虚拟机(Kernel-based Virtual Machine),它是一个 Linux 的一个内核模块,该内核模块使得 Linux 变成了一个 Hyperv ...
- pandas-13 时间序列操作方法pd.date_range()
pandas-13 时间序列操作方法pd.date_range() 在pandas中拥有强大的时间序列操作方法. 使用 pd.date_range() 生成 'pandas.core.indexes. ...
- MySQLNonTransientConnectionException: Could not create connection to database server.
MySQLNonTransientConnectionException: Could not create connection to database server. Spring整合mybati ...