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 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.
思路:
记录左边,枚举右边。。。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 2e5+10;
int a[MAXN];
map<int, int> Mp;
int n;
int main()
{
int t;
cin >> t;
while(t--)
{
Mp.clear();
cin >> n;
for (int i = 1;i <= 2*n;i++)
cin >> a[i];
int sum = 0;
Mp[0] = 0;
for (int i = 1;i <= n;i++)
{
if (a[i] == 1)
sum++;
else
sum--;
Mp[sum] = i;
}
sum = 0;
int ans = 2*n;
ans = min(ans, 2*n-Mp[0]);
for (int i = 2*n;i >= n+1;i--)
{
if (a[i] == 1)
sum++;
else
sum--;
if (sum == 0)
ans = min(ans, i-Mp[0]-1);
if (Mp[0-sum] != 0)
ans = min(ans, i-Mp[0-sum]-1);
}
cout << ans << endl;
}
return 0;
}
Educational Codeforces Round 78 (Rated for Div. 2) C. Berry Jam的更多相关文章
- Educational Codeforces Round 78 (Rated for Div. 2) C - Berry Jam(前缀和)
- 【cf比赛记录】Educational Codeforces Round 78 (Rated for Div. 2)
比赛传送门 A. Shuffle Hashing 题意:加密字符串.可以把字符串的字母打乱后再从前面以及后面接上字符串.问加密后的字符串是否符合加密规则. 题解:字符串的长度很短,直接暴力搜索所有情况 ...
- Educational Codeforces Round 78 (Rated for Div. 2) D. Segment Tree
链接: https://codeforces.com/contest/1278/problem/D 题意: As the name of the task implies, you are asked ...
- Educational Codeforces Round 78 (Rated for Div. 2) B. A and B
链接: https://codeforces.com/contest/1278/problem/B 题意: You are given two integers a and b. You can pe ...
- Educational Codeforces Round 78 (Rated for Div. 2) A. Shuffle Hashing
链接: https://codeforces.com/contest/1278/problem/A 题意: Polycarp has built his own web service. Being ...
- Educational Codeforces Round 78 (Rated for Div. 2)B. A and B(1~n的分配)
题:https://codeforces.com/contest/1278/problem/B 思路:还是把1~n分配给俩个数,让他们最终相等 假设刚开始两个数字相等,然后一个数字向前走了abs(b- ...
- Educational Codeforces Round 78 (Rated for Div. 2)
A题 给出n对串,求s1,是否为s2一段连续子串的重排,串长度只有100,从第一个字符开始枚举,sort之后比较一遍就可以了: char s1[200],s2[200],s3[200]; int ma ...
- Educational Codeforces Round 78 (Rated for Div. 2) --补题
链接 直接用数组记录每个字母的个数即可 #include<bits/stdc++.h> using namespace std; int a[26] = {0}; int b[26] = ...
- Educational Codeforces Round 78 (Rated for Div. 2) 题解
Shuffle Hashing A and B Berry Jam Segment Tree Tests for problem D Cards Shuffle Hashing \[ Time Lim ...
随机推荐
- 代码移植的福音 namespace_alias
命名空间别名 允许程序员定义命名空间的另一个名字 它们常用作长的或嵌套过深的命名空间的简便使用方式. 我们也可以将用在代码移植上,而无需修改源代码的文件所定义的命名空间, 为后面升级merge代码创造 ...
- K8S 如何实现将git代码下拉到指定的容器路径中
gitRepo 是 kubernetes Volume类型中的一种,gitRepo volume可以实现将git代码下拉到指定的容器路径中. 备注:实现此功能,Pod运行的节点都必需要安装git.换句 ...
- python 正则表达式匹配IP地址
一.实验环境 1.Windows7x64_SP1 2.anaconda2.5.0 + python2.7(anaconda集成,不需单独安装) 3.pyinstaller3.0 二.实验目的 从tex ...
- java核心技术(第十版卷一)笔记(纯干货!)
这是我读过的第三本关于java基础的书.第一本<<java从入门到精通>>这本书让我灵识初开.第二本<<java敏捷开发>>这本书则是有一次被一位师傅批 ...
- 解决xunsearch热门搜索,不按照数量排序问题
public function getHotQuery($limit = 6, $type = 'total') { $ret = array(); $limit = max(1, min(50, i ...
- [转帖]CHROME开发者工具的小技巧
CHROME开发者工具的小技巧 https://coolshell.cn/articles/17634.html 需要仔细学习看一看呢. 2017年01月19日 陈皓 评论 58 条评论 64,08 ...
- java 解决safari下载中文文件名乱码
主要就是在响应头设置content-disposition,主要遵循 RFC 5987标准. response.setHeader("content-disposition",&q ...
- VSCode打字特效Power Mode插件
由于最近比较频繁使用VSCode这个软件写代码,然后里面有一个非常炫酷的打字特效插件,平时写代码的时候不会感觉太枯燥(其实就是装一下逼吧)! 安装很简单,但是容易忘,所以这里整理一下具体的部署步骤. ...
- Linux : Nginx相关
nginx安装参考链接: https://www.cnblogs.com/kaid/p/7640723.html 自定义编译目录: https://blog.csdn.net/ainuser/arti ...
- 《 .NET并发编程实战》阅读指南 - 第2章
先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.