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 ...
随机推荐
- unity回调函数范例
using System.Collections; using System.Collections.Generic; using UnityEngine; public class callback ...
- php扩展安装phpize
安装php(fastcgi模式)的时候,常常有这样一句命令:/usr/local/webserver/php/bin/phpize 一.phpize是干嘛的? phpize是什么东西呢?php官方的说 ...
- 解决from lxml import etree 导入的时候,显示etree不存在
问题: 当安装完lxml之后,发现使用 from lxml import etree 时,etree不可用 原因 :是lxml中没有etree包 解决: 去官网下载对应包:官网地址:http://l ...
- js校验密码强度
网上转载的一段代码,留着以后用, js文件: //判断输入密码的类型 function CharMode(iN){ if (iN>=48 && iN <=57) //数字 ...
- 【问题解决】Tomcat 启动时闪退或提示“Neither the JAVA_HOME or the JRE_HOME environmental variable is defined.”
问题解决思路: 1.分析startup.bat启动脚本:发现其调用了catalina.bat,而catalina.bat调用了setclasspath.bat 2.在setclasspath.bat的 ...
- 右键打开cmd
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shell\OpenCmdHere]@="Open cmd ...
- Mybatis sql注入问题
预编译方式,即PreparedStatement,可以防注入:#{id} <select id="getBlogById" resultType="Blog&quo ...
- FTP开启被动连接模式
在Linux环境下搭建ftp服务器,具体步骤见:http://www.cnblogs.com/zjiacun/p/6896803.html 配置被动连接的方法: 找到配置文件/etc/vsftpd/v ...
- 添加启动项及常用Windows+R
常用Windows+R services.msc---本地服务设置 msconfig---系统配置实用程序 mspaint--------画图板 notepad--------打开记事本 Nslook ...
- C# 串口调试助手源码
本方法,禁用跨进程错误(做法不太好,但是对于单片机出身的人来说,好理解,能用就行). 基本功能: 1.点串口号的下拉菜单自动当前检索设备管理器的COM 2.发送模式可选,hex和string两种 3. ...