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 ...
随机推荐
- LInux 就该这么学 笔记分享
看了Linux就该这么学的前部分书,觉得写的还可以,就在网上找了下面这个同学写的笔记,觉得很详细,所以保存地址,供以后查阅参看.这里对作者表示感谢!!! 博客地址: https://www.cnblo ...
- 转: 彻底理解 Spring 容器和应用上下文
本文由 简悦 SimpRead 转码, 原文地址 https://mp.weixin.qq.com/s/o11jVTJRsBi998WlgpfrOw 有了 Spring 之后,通过依赖注入的方式,我们 ...
- java中的7个位运算运算符
位运算指的是针对整数的二进制进行的位移操作. 位运算提供比算术运算更高的效率,但是位运算的代码可读性较差,建议所有使用位运算的地方写上注释. Java中提供7个位运算符用于位运算. 左移(<&l ...
- Redux-saga-整理
介绍 在redux中更好的解决异步操作 redux-saga相当于在redux原来的数据流中多了一层,对action进行监听 接收到action时,派发一个任务维护state saga通过Genera ...
- Linq与委托
using System; using System.Linq; using System.Reflection; using Stuglxt_Models; namespace ConsoleApp ...
- ABP——切换MySQL数据库
我是一名.net新手,应公司要求开始学习.net,使用的是土耳其大牛写的框架ASP.NET Boilerplate 简称ABP,是基于DDD的现代ASP.NET开发框架,ABP提供了一个启动模板用于新 ...
- Delphi - 10进制16进制相互转换
10进制转16进制 使用IntToHex可以实现十进制到十六进制的转换,注意这里的参数有两个,第一个表示需要被转换的10进制数,第二个表示转换后用几位来显示16进制数. 代码如下: function ...
- Bootstrap3-导航
Bootstrap 导航 1. 定义导航组件 基本结构: <!-- 基本导航组件 --> <ul class="nav"> <li class=&qu ...
- js数组去重 数组拼接 替换数组中的指定值 递归数组 判断数组中是否存在指定值 数组求和 根据条件判数组值
这是学习过程中记录的一些关于数组操作的常用属性或方法,记录一下方便以后使用. // 数组去重 var arr1 = [1,1,2,3,4,5,6,3,2,4,5,'a','b','c','a',6,7 ...
- 英语chiltonite葡萄石chiltonite单词
葡萄石Chiltonite 1.葡萄石能够促进血液循环,具有美容养颜的功效,非常适合女性佩戴,可以增加个人魅力,还能加强事业财运,凝聚财富气场. 2.绿色光对应人体心轮,对心脏,肺脏有效用,内涵的磁石 ...