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 ...
随机推荐
- xpath 获取父级,和同级
XPath轴(XPath Axes)可定义某个相对于当前节点的节点集: 1.child 选取当前节点的所有子元素 2.parent 选取当前节点的父节点 3.descendant 选取当前节点的所有后 ...
- jquery 使用笔记
一下是在做项目中用到jquery涉及到的一些知识点,把源码复制过来,省得以后忘记了: <link href="<%=request.getContextPath()%>/c ...
- SDOI2012 Round1 day2 集合(set)解题报告
//=====================以上为官方题解==============// 数据略水,暴力枚举50. 把边按照升序排一遍,在询问,水过. #include<cstdio> ...
- 九度OJ 1334:占座位 (模拟)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:864 解决:202 题目描述: sun所在学校的教室座位每天都是可以预占的. 一个人可以去占多个座位,而且一定是要连续的座位,如果占不到他所 ...
- json.dumps(i['bd_res'], ensure_ascii=False)
json.dumps(i['bd_res'], ensure_ascii=False) import xlrd import time import sys import os import requ ...
- 【学员管理系统】0x02 学生信息管理功能
[学员管理系统]0x02 学生信息管理功能 写在前面 项目详细需求参见:Django项目之[学员管理系统] Django框架大致处理流程 捋一下Django框架相关的内容: 浏览器输入URL到页面展示 ...
- 我的Android进阶之旅------>如何为ListView组件加上快速滑块以及修改快速滑块图像
使用布局文件需要将android:fastScrollEnabled="true" ,如下代码所示: <ListView android:id="@+id/list ...
- 聊聊数据库~6.SQL运维中篇
上篇回顾:https://www.cnblogs.com/dotnetcrazy/p/10810798.html#top 1.6.5.MySQL日志相关 本文的测试环境:MySQL5.7.26.Mar ...
- memcached 不同客户端的问题
摘要: memcached-java客户端调用get方法获取数据失败 主要演示一下在memcached服务器端set数据之后,在客户端调用java api获取数据.不过此过程如果不慎会读取数据失败. ...
- 海信电视 LED55K370 升级固件总结【含固件下载地址】
最早电视买回来,感觉垃圾软件太多,root后,删软件不小心删除了桌面,导致没桌面. 用ADB装了点软件,凑合可以用. 后来装了悟空遥控,然后装了沙发桌面,不影响使用了. 最近海信不停推送更新系统,改手 ...