Codeforces 632B Alice, Bob, Two Teams 【水题】
2016-03-04 22:09
453 查看
B. Alice, Bob, Two Teams
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
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 ncharacter 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.
Examples
input
output
input
output
input
output
Note
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.
题意:给定n个数和一个字符串,每个数对应一个字符,A表示属于Alice,B表示属于Bob。Bob可以选出字符串的前缀 或者 一个后缀来翻转,A -> B, B -> A,只能使用一次。问Bob可以获得的最大值。(所有属于Bob的数之和)。
思路:维护每一位的前缀和 和 后缀和,最后再是不翻转的情况。
AC代码:
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
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 ncharacter 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.
Examples
input
5 1 2 3 4 5 ABABA
output
11
input
5 1 2 3 4 5 AAAAA
output
15
input
1 1 B
output
1
Note
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.
题意:给定n个数和一个字符串,每个数对应一个字符,A表示属于Alice,B表示属于Bob。Bob可以选出字符串的前缀 或者 一个后缀来翻转,A -> B, B -> A,只能使用一次。问Bob可以获得的最大值。(所有属于Bob的数之和)。
思路:维护每一位的前缀和 和 后缀和,最后再是不翻转的情况。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define ll o<<1#define rr o<<1|1using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const int MAXN = 5*1e5+10;
const int INF = 0x3f3f3f3f;
void add(LL &x, LL y) {x += y; x %= MOD;}
int a[MAXN];
char str[MAXN];
LL sum[MAXN];
int main()
{
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
cin >> str+1; sum[0] = 0;
for(int i = 1; i <= n; i++)
{
if(str[i] == 'B')
sum[i] = sum[i-1] + a[i];
else
sum[i] = sum[i-1];
}
LL res = 0; LL ans = 0;
for(int i = 1; i <= n; i++)
{
if(str[i] == 'A')
res += a[i];
ans = max(ans, res + sum
- sum[i-1]);
}
res = 0;
for(int i = n; i >= 1; i--)
{
if(str[i] == 'A')
res += a[i];
ans = max(ans, res + sum[i-1]);
}
cout << max(ans, sum
) << endl;
return 0;
}
相关文章推荐
- 自定义View中onMeasure在wrap_content时的处理
- android播放多张图片形成动画 (帧动画)
- BZOJ4385/POI2015Wilcze doły
- IOS 世界标准时间UTC /GMT 转为当前系统时区对应的时间
- Codeforces 632A Grandma Laura and Apples 【水题】
- lightoj 1282 - Leading and Trailing 【数学】
- DMA2D 原理
- [LeetCode234]Palindrome Linked List
- BZOJ 1022 小约翰的游戏
- js 原生对象排序
- C#旅途 ----趁热打铁 活学活用 数组快排,查找,拷贝;
- nyoj 104 最大和
- 寒假刷题—栈和队列
- 阅读计划
- Collections中sort()方法源代码的简单分析
- socket.io搭建分布式web推送服务器
- 关于欧拉筛求phi的讨论
- MySQL常见性能命令
- MySQL数据库服务器优化详细
- STM32f746G-DISCO开发使用