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 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.
Input
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).
Output
Print the only integer a — the maximum strength Bob can achieve.
Sample Input
5
1 2 3 4 5
ABABA
Sample Output
11
Hint
题意
有n个物品,每个物品有一个分值
然后每个物品有一个标牌,如果标牌上是A,那么就属于A,是B,那么就属于B
现在你可以选择一个前缀或者后缀,然后将上面的A改成B,将B改成A
然后问你最多B能够得到多少
题解:
暴力维护前缀和就好了
维护A的前缀和,和B的前缀和
翻转其实就是把A的变成B,B的变成A而已
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+7;
int n;
long long p[maxn];
char s[maxn];
long long sum1[maxn];
long long sum2[maxn];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&p[i]);
scanf("%s",s+1);
for(int i=1;i<=n;i++)
{
sum1[i]=sum1[i-1];
sum2[i]=sum2[i-1];
if(s[i]=='A')
sum1[i]+=p[i];
else
sum2[i]+=p[i];
}
long long ans = max(sum2[n],sum1[n]);
for(int i=1;i<=n;i++)
{
ans = max(ans,sum2[n]-sum2[i]+sum1[i]);
ans = max(ans,sum2[i]+sum1[n]-sum1[i]);
}
cout<<ans<<endl;
}
Educational Codeforces Round 9 B. Alice, Bob, Two Teams 前缀和的更多相关文章
- Educational Codeforces Round 129 (Rated for Div. 2) A-D
Educational Codeforces Round 129 (Rated for Div. 2) A-D A 题目 https://codeforces.com/contest/1681/pro ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- Educational Codeforces Round 9
Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
随机推荐
- 第一章:读取文件一行IO::File
#!c:\\perl\\bin\\perl.exe use IO::File; #读取一行 my $fd = IO::File->new('perl.txt'); my $one_line = ...
- 2.C 基础
C 基础 原文地址:http://rypress.com/tutorials/objective-c/c-basics OC 可以说是C语言的一个超集,这样你可以无缝的和C语言结合编程也就是你可以这两 ...
- LCT 文档
file:///C:/Users/Frank/Downloads/QTREE%E8%A7%A3%E6%B3%95%E7%9A%84%E4%B8%80%E4%BA%9B%E7%A0%94%E7%A9%B ...
- java中的数组与集合相互转换
1.数组转换成集合 数组转换为集合,用Arrays.asList方法. public static void main(String[] args) { String[] arr = {"a ...
- java常用设计模式学习心得
学习自:http://shenzhenchufa.blog.51cto.com/730213/161581 代码来自:http://shenzhenchufa.blog.51cto.com/73021 ...
- lr_Analysis Options选项介绍
- .net mvc禁用浏览器缓存
我正在寻找方法来禁用 整个 ASP.Net MVC 网站 的浏览器缓存 我发现以下方法, Response.Cache.SetCacheability(System.Web.HttpCacheabil ...
- LCA算法笔记
LCA,最近公共祖先,实现有多种不同的方法,在树上的问题中有着广泛的应用,比如说树上的最短路之类. LCA的实现方法有很多,比如RMQ.树链剖分等. 今天来讲其中实现较为简单的三种算法: RMQ+时间 ...
- 字典树&01字典树算法笔记
1]学习了字典树之后,觉得它很明显的就是用空间来换时间,空间复杂度特别大,比如字典数单单存26个小写字母,那么每个节点的孩子节点都有26个孩子节点,字典树中的每一层都保留着不同单词的相同字母. 2]0 ...
- HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...