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取绝对值最小值时,需要加和的数字的个数. 题目分析 需 ...
随机推荐
- 一文快速入门Docker
Docker提供一种安全.可重复的环境中自动部署软件的方式,拉开了基于与计算平台发展方式的变革序幕.如今Docker在互联网公司使用已经非常普遍.本文用十分钟时间,带你快速入门Docker. Dock ...
- tkinter基础-输入框、文本框
本节内容 了解输入框.文本框的使用方法 利用1制作简易界面 首先明确上面由几个元素组成:该界面由界面标题,输入框.两个按钮.文本框组成. 该界面我们需要实现的功能: 在输入框中输入文字,点击inser ...
- nginx+lua访问流量实时上报kafka
在nginx这一层,接收到访问请求的时候,就把请求的流量上报发送给kafka storm才能去消费kafka中的实时的访问日志,然后去进行缓存热数据的统计 从lua脚本直接创建一个kafka prod ...
- springboot 通过@WebFilter(urlPatterns )配置Filter过滤路径
springboot 通过@WebFilter(urlPatterns )配置Filter过滤路径,没有配置/*,输入任何路径都能进过滤器 2019年04月25日 12:51:33 peigui.hu ...
- 在虚拟机Linux安装Redis
在虚拟机上安装 CentOS 7 安装成功后登录Root用户进入 opt目录,下载Redis. 下载Redis 下载命令: wget http://download.redis.io/releases ...
- java之spring之依赖注入
一.DI: Dependency injection; 依赖注入 依赖注入和控制反转是同一个概念的不同说法. 对象的创建依赖于容器.对象属性的设置是由容器来设置. 对象属性的赋值过程称为注入. 二.S ...
- Java之路---Day06
2019-10-20-23:13:52 目录内容: 1.Scanner类 2.匿名对象 3.Random类 4.对象数组 Scanner类 功能:实现键盘输入数据到程序中 使用步骤: 1.导包: im ...
- ASPxComboBox默认情况下不显示代码和名称,特别头疼,直到发现了关键
1.ASPxComboBox 默认不开启 AutoPostBack: 既是开启,总显示第一行(好像是个bug) 2.只好ajax,但是默认情况下不显示代码和名称,特别头疼,直到发现了关键 <dx ...
- JavaScript学习笔记(6月份)
由于笔记比较杂,本身学习程度并不理想,所以暂时没有整理这些繁杂的笔记. ps:博客园markdown用起来和看起来都舒服太多了,这才是我了解的那个markdown,又回来了! 笔记 DOM对象 doc ...
- 填坑——audio不能正常播放,控制台报错 Uncaught (in promise) DOMException
原文:https://blog.csdn.net/Mariosss/article/details/87861167 用chrome调试页面时,发现audio控件有时不能正常播放音频,控制台报错 Un ...