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 ...
随机推荐
- OSG :三维无序离散点构建Delaunay三角网
利用OSG的osgUtil库里面的DelaunayTriangulator类. points是需要构建三角网的点 osgUtil::DelaunayTriangulator* trig = new o ...
- SPA框架 Angular、React、Vue
指尖前端重构(React)技术调研分析 摘要:重构前的技术文档调研与分析,包括技术选型为什么选择react,应用过程中的注意事项等. 一.为什么选择React React是当前前端应用最广泛的框架 ...
- Java8 新特性 Stream 无状态中间操作
无状态中间操作 Java8 新特性 Stream 练习实例 中间无状态操作,可以在单个对单个的数据进行处理.比如:filter(过滤)一个元素的时候,也可以判断,比如map(映射)... 过滤 fil ...
- 【模板整合计划】NB数论
[模板整合计划]NB数论 一:[质数] 1.[暴力判] 素数.コンテスト.素数 \(\text{[AT807]}\) #include<cstdio> #include<cmath& ...
- 利用kibana学习 elasticsearch restful api (DSL)
利用kibana学习 elasticsearch restful api (DSL) 1.了解elasticsearch基本概念Index: databaseType: tableDocument: ...
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1 in position 3: invalid start byte错误解决办法
这类错误的原因是编码造成的,通常情况下都是utf-8编码,这需要变换一下,改成encoding="ISO-8859-1"即可: file = pd.read_csv("/ ...
- MySql 获取数据库的所有表名
目录 写在前面 根据数据库获取该数据库下所有的表名 根据表名获取列名与列值 写在前面 在实现某个功能的时候,需要使用MySql数据库获取某数据的所有的表名以及该表名的所有列名与列值. 根据数据库获取该 ...
- 2019 农信互联java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.农信互联等公司offer,岗位是Java后端开发,因为发展原因最终选择去了农信互联,入职一年时间了,也成为了面 ...
- java中的%取模
在java中的 % 实际上是取余. 下面为数学概念上的取余和取模: 对于整型数a,b来说,取模运算或者求余运算的方法都是: 1.求 整数商: c = a/b; 2.计算模或者余数: r = a - ...
- 获取post传输参数
1.获取post参数可以用 传输参数为 a=aa&b=bb这种 public static SortedDictionary<string, string> GetRequestP ...