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 ...
随机推荐
- Linux64位程序移植
1 概述 Linux下的程序大多充当服务器的角色,在这种情况下,随着负载量和功能的增加,服务器所使用内存必然也随之增加,然而32位系统固有的4GB虚拟地址空间限制,在如今已是非常突出的问题了:另一个需 ...
- Linux进程间通信(四) - 共享内存
共享内存的优势 采用共享内存通信的一个显而易见的好处是效率高,因为进程可以直接读写内存,而不需要任何数据的拷贝.对于像管道和消息队列等通信方式,则需要在内核和用户空间进行四次的数据拷贝,而共享内存则只 ...
- Http权威指南学习研究
学习时间: 该学习:第六章 6.6小节 加油 185页 2017年5月15日15:13:00 今天任务: 看完前两章节: ...
- WebApi 中使用 Session
1. 在 Global.asax.cs 文件中加入session支持 protected void Application_Start() { AreaRegistration.RegisterAll ...
- 九度OJ 1344:可乐瓶展览 (DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:430 解决:76 题目描述: 众所周知JOBDU旗下的JOBCOLA公司是文明全球的著名可乐制造商,与其它可乐公司不同的是,JOBCOLA可 ...
- 九度OJ 1341:艾薇儿的演唱会 (最短路)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:528 解决:241 题目描述: 艾薇儿今天来到了中国,她计划两天后在哈尔滨举行一场个人的演唱会.由于出现了紧急情况,演唱会的举办方要求艾薇儿 ...
- hibernate多对多关系配置
一.创建用户,角色实体类. 一名用户可以有多个角色.一个角色可以对于多名用户. 用户实体类 public class User { private int uId; private String uN ...
- JVM类加载流程
1.加载 a.装载类的第一个阶段 b.取得类的二进制流 c.转为方法区数据结构 d.在Java堆中生成对应的java.lang.Class对象 2.链接 a.验证(保证Class流的格式是正确的) 文 ...
- linux 9 -- 交互式使用Bash Shell
二十二. 交互式使用Bash Shell: 1. 用set命令设置bash的选项: 下面为set主要选项的列表及其表述: 选项名 开关缩写 描述 allexport -a 打开此开关 ...
- 【WEB】高并发Web服务的演变-节约系统内存和CPU
目前主流浏览器通常可以存在2-6个并发. 连接和请求,占据了服务器的大量CPU和内存等资源.在资源数目超过100+的网站页面中,使用更多的下载连接,非常有必要. 缓解“高并发”的压力的手段. 一. W ...