codeforces 632B B. Alice, Bob, Two Teams(暴力)
1.5 seconds
256 megabytes
standard input
standard output
Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi.
The way to split up game pieces is split into several steps:
- First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B.
- Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once.
- Alice will get all the pieces marked A and Bob will get all the pieces marked B.
The strength of a player is then the sum of strengths of the pieces in the group.
Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve.
The first line contains integer n (1 ≤ n ≤ 5·105) — the number of game pieces.
The second line contains n integers pi (1 ≤ pi ≤ 109) — the strength of the i-th piece.
The third line contains n characters A or B — the assignment of teams after the first step (after Alice's step).
Print the only integer a — the maximum strength Bob can achieve.
5
1 2 3 4 5
ABABA
11
5
1 2 3 4 5
AAAAA
15
1
1
B
1
In the first sample Bob should flip the suffix of length one.
In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.
In the third sample Bob should do nothing.
题意:最多改变一次,改变为A变为B,B变为A,且为前缀或者后缀,问B的最大的和是多少;
思路:找出所有的前缀和后缀的A的和和B的和,暴力找最大值;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+3;
long long p[5*N],pa[5*N],pb[5*N],sa[5*N],sb[5*N];
int n;
char str[5*N];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
cin>>p[i];
}
scanf("%s",str+1);
pa[0]=0;
pb[0]=0;
for(int i=1;i<=n;i++)
{
if(str[i]=='A')
{
pa[i]=pa[i-1]+p[i];
pb[i]=pb[i-1];
}
else
{
pa[i]=pa[i-1];
pb[i]=pb[i-1]+p[i];
}
}
sa[n+1]=0;
sb[n+1]=0;
for(int i=n;i>0;i--)
{
if(str[i]=='A')
{
sa[i]=sa[i+1]+p[i];
sb[i]=sb[i+1];
}
else
{
sa[i]=sa[i+1];
sb[i]=sb[i+1]+p[i];
}
}
long long ans=0,x=0;
for(int i=0;i<=n;i++)
{
x=max(sa[i+1],sb[i+1]);
ans=max(ans,x+pb[i]);
}
for(int i=n+1;i>0;i--)
{
x=max(pa[i-1],pb[i-1]);
ans=max(ans,x+sb[i]);
}
cout<<ans<<endl;
return 0;
}
codeforces 632B B. Alice, Bob, Two Teams(暴力)的更多相关文章
- Educational Codeforces Round 9 B. Alice, Bob, Two Teams 前缀和
B. Alice, Bob, Two Teams 题目连接: http://www.codeforces.com/contest/632/problem/B Description Alice and ...
- 【Henu ACM Round #12 C】 Alice, Bob, Two Teams
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑任意两个字符串(a,b) 假设a在b的前面 那么如果a+b>=b+a 这里的+表示字符串的链接 那么显然需要交换a,b的位 ...
- 【Henu ACM Round #12 B】 Alice, Bob, Two Teams
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 写个前缀和 和 一个后缀和. (即前i个字符A所代表的数字的和以及前i个字符B所代表的数字的和.. 然后枚举前i个字符翻转. 求B对 ...
- Alice, Bob, Oranges and Apples CodeForces - 586E
E - Alice, Bob, Oranges and Apples CodeForces - 586E 自己想的时候模拟了一下各个结果 感觉是不是会跟橘子苹果之间的比例有什么关系 搜题解的时候发现了 ...
- Codeforces Beta Round #6 (Div. 2 Only) C. Alice, Bob and Chocolate 水题
C. Alice, Bob and Chocolate 题目连接: http://codeforces.com/contest/6/problem/C Description Alice and Bo ...
- CF6C Alice, Bob and Chocolate
CF6C Alice, Bob and Chocolate 题目链接 写了一天搜索写的有点累了,就顺手水了一道CF的模拟题 这道题就是简单的模拟整个题的过程,注意最后输出的形式就好了QWQ AC代码如 ...
- CodeForces 1249A --- Yet Another Dividing into Teams
[CodeForces 1249A --- Yet Another Dividing into Teams] Description You are a coach of a group consis ...
- Codeforces Gym 100803F There is No Alternative 暴力Kruskal
There is No Alternative 题目连接: http://codeforces.com/gym/100803/attachments Description ICPC (Isles o ...
- Codeforces Beta Round #13 E. Holes 分块暴力
E. Holes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/13/problem/E Des ...
随机推荐
- python 文字识别 之 pytesseract
pytesseract资源 链接:https://pan.baidu.com/s/1eTsqhsY 密码:j0yo 安装时前面一直next就可以了,直到这一步,勾选Math和Chinese,支持计算和 ...
- nginx日志自动切分
#!/bin/bash NGINX_LOG_PATH=/data/nginx-/logs # 昨天 YESTERDAY=$(date -d "yesterday" +%Y-%m-% ...
- Pipeline outbound
netty源码死磕8 Pipeline outbound 出站流程揭秘 1. Pipeline outbound流程 1.1. 出站的定义 简单回顾一下. 出站(outbound) 操作,通常是处于上 ...
- [IOI2018]组合动作
IOI2018 组合动作 UOJ 首先显然可以两次试出首字母 考虑增量构造 假设首字母为A,且已经试出前i个字母得到的串s 我们考虑press这样一个串s+BB+s+BX+s+BY+s+XA 首先这个 ...
- Django 之 ModelForm 组件
Django的model form组件 扩展:Django 之Form组件 首先我们要知道 Model 和 Form 分别时干什么的 Model 生成表数据 Form 对表单.字段进行校验 Dja ...
- RxJava2 源代码解析(一)
转载请标明出处: http://blog.csdn.net/zxt0601/article/details/61614799 本文出自:[张旭童的博客](http://blog.csdn.net/zx ...
- Linux显示网络相关信息
netstat -tlun 查看本机监听的端口 netstat -an 查看本机所有的网络连接 netstat -rn 查看本机路由表 -t TCP协议 -u UDP协议 - ...
- VLFeat图像库在VS2012下的配置
近期做课题所需,開始使用VLFeat图像库. 库下载链接: http://download.csdn.net/detail/sunboyiris/7500097 ...
- 变分推断(Variational Inference)
变分 对于普通的函数f(x),我们可以认为f是一个关于x的一个实数算子,其作用是将实数x映射到实数f(x).那么类比这种模式,假设存在函数算子F,它是关于f(x)的函数算子,可以将f(x)映射成实数F ...
- Java mail 发送邮件 主题(标题)乱码
最近开发遇到Javamail 发送邮件标题乱码问题,腾讯.网易邮箱不会乱码,阿里邮箱 标题则会乱码.解决办法: String subject = MimeUtility.encodeWord(ma ...