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 ...
随机推荐
- C#自定义特性的使用
特性类的使用过程: 第一步:定义一个特性类,定义一些成员来包含验证时需要的数据:第二步:创建特性类实例:创建一个特性类的实例,里面包含着验证某一个属性或者字段需要的数据.将该实例关联到某个属性上面.第 ...
- 在Visual Studio中新增生成项目
在Visual Studio中新增生成项目 选择适配器类型 选择WCF-SQL适配器 创建连接选项 选择相应的存储过程 生成相应的消息架构
- mqtt数据采集器
MQTT是一种发布(publish)/订阅(subscribe)协议,MQTT协议采用发布/订阅模式,所有的物联网终端都通过TCP连接到云端,云端通过主题的方式管理各个设备关注的通讯内容,负责将设备与 ...
- MyBatis 通过 BATCH 批量提交
本文由 简悦 SimpRead 转码, 原文地址 https://www.jb51.net/article/153382.htm 很多人在用 MyBatis 或者 通用 Mapper 时,经常会问有没 ...
- Appium+python自动化(五)- 模拟器(超详解)
简介 Appium是做安卓自动化的一个比较流行的工具,对于想要学习该工具但是又局限于或许当前有些小伙伴没 android 手机来说,可以通过安卓模拟器来解决该问题,下面就讲解使用appium连接安卓模 ...
- 【学习笔记】薛定谔的喵咪Cat—球盒问题(全详解)
[学习笔记]薛定谔的喵咪Cat-球盒问题(全详解) [题目描述] 当一个猫在盒子里时,因为放射物的状态我们不知道,所以猫的状态我们也不知道,这就所谓猫的生死纠缠态,也是所谓的薛定谔的猫. 当我们做需要 ...
- 很好的OpenCV入门资料
https://files.cnblogs.com/files/mqingqing123/OpenCV%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B.rar
- .NET MVC 序列化与反序列化
using System.Runtime.Serialization.Json; using System.IO; using System.Text; //序列化 public str ...
- EIP Core2.0开源
EIP Core2 权限管理系统 (交流群:495070603,作者:1039318332) 开源地址: https://gitee.com/sunzewei/eipcore2 https://git ...
- Spring.yml配置文件读取字符串出现错误
今天遇到一个诡异的问题,在配置文件中配置了一个值为字符串的属性,但是在用@Value注入时发现注入的值不是我配置的值,而且在全文都没有找到匹配的值 之后研究了好久,发现yml文件会把0开头的数组进行8 ...