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 ...
随机推荐
- 【SSH进阶之路】Hibernate基本映射(三)
[SSH进阶之路]Hibernate基本原理(一) ,小编介绍了Hibernate的基本原理以及它的核心.採用对象化的思维操作关系型数据库. [SSH进阶之路]Hibernate搭建开发环境+简单实例 ...
- vptr
#include <stdio.h> class Point3d { public: virtual ~Point3d(){} public: static Point3d origin; ...
- 1 了解Scala
1 定义变量 单个变量:var name = "benxintuzi" 等价于 var name : String = "benxintuzi"(即定义变量时 ...
- 【BZOJ2844】albus就是要第一个出场 高斯消元求线性基
[BZOJ2844]albus就是要第一个出场 Description 已知一个长度为n的正整数序列A(下标从1开始), 令 S = { x | 1 <= x <= n }, S 的幂集2 ...
- mysql存储过程之事务篇
mysql存储过程之事务篇 事务的四大特征: ACID:Atomic(原子性).Consistent(一致性).Isolated(独立性).Durable (持久性) MySQL的事务支持不是绑定在M ...
- Ogbect对象转换为泛型对象
相信很多人都自己写个这个转换的方法,再次附上我自己的写转换方法仅供参考. T t = BeanUtil.dbObject2Bean(obj, tClass); public static <T& ...
- 6.2.1-FactoryBeanRegistrySupport(未全)
FactoryBeanRegistrySupport 的关系图: 添加工厂方式创建类FactoryBean的支持
- 像使用linux一样使用mac
1 不能像使用windows一样使用mac 因为mac卸载不方便. 2 gcc的问题 就使用系统默认的gcc,即clang,要想使用原声的gcc是不行的,mac本身不支持.
- JS异错面试题
转自 http://www.codeceo.com/article/one-javascript-interview.html function Foo() { getName = function ...
- 从 零开始 无差错 装好nginx+PHP
由于这两天 一直有人追问 nginx为何报错,为何php没装好啥的,大多原因是 : 1.编译与yum混合安装,导致很多包的路径不对,进而报错 2.yum源比较旧,导致 与新版本的php不匹配 3.安装 ...