Alice, Bob, Two Teams CodeForces
来源:互联网 发布:09和冷冷的故事 知乎 编辑:程序博客网 时间:2024/06/09 22:12
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.
Example
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
题解:
字符串内的A,B可以从前或从后开始改变任意长度的前缀或后缀(A->B,B->A).使最终的B的和最大。
只需要对不同的情况暴力找出最优解就行了。
代码如下:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define maxn 500005using namespace std;typedef long long ll; //刚开始用的是int,结果错了,一想原来超限了。ll a[maxn]; //存输入的每个对应的值。char s[maxn]; //存A,B字符串。ll sumA[maxn]={0}; //存一段字符串中A对应的值之和。ll sumB[maxn]={0}; //存一段字符串中B对应值之和。int main(){ ll n; while(~scanf("%lld",&n)){ for(ll i=1;i<=n;i++) scanf("%lld",&a[i]); scanf("%s",s+1); for(ll i=1;i<=n;i++){ //统计每一段字符串中A,B对应的值。 sumA[i]=sumA[i-1]; sumB[i]=sumB[i-1]; if(s[i]=='A'){ sumA[i]=sumA[i]+a[i]; }else{ sumB[i]=sumB[i]+a[i]; } } ll ans=max(sumA[n],sumB[n]); //记录最大值。 for(ll i=1;i<=n;i++){ ans=max(ans,sumB[n]-sumB[i]+sumA[i]); ans=max(ans,sumA[n]-sumA[i]+sumB[i]); } printf("%lld\n",ans); } return 0;}
其中ans=max(ans,sumB[n]-sumB[i]+sumA[i]);统计前缀变时的值,不断更新最大值。
其中ans=max(ans,sumA[n]-sumA[i]+sumB[i]);统计后缀变时的值,不断更新最大值。
- Alice, Bob, Two Teams CodeForces
- codeforces--623B Alice, Bob, Two Teams
- CodeForces 632B- Alice, Bob, Two Teams
- codeforces-632B-Alice, Bob, Two Teams
- CodeForces-632B-Alice, Bob, Two Teams
- CodeForces 632B Alice, Bob, Two Teams
- CodeForces 632B Alice, Bob, Two Teams
- 【codeforces 632B Alice, Bob, Two Teams】
- 【CodeForces】 632B Alice, Bob, Two Teams
- CodeForces 632B Alice, Bob, Two Teams
- CodeForces 632B Alice, Bob, Two Teams
- Alice, Bob, Two Teams codeforces 632B
- Alice, Bob, Two Teams
- Alice, Bob, Two Teams
- Alice, Bob, Two Teams
- codeforces_632B. Alice, Bob, Two Teams
- Educational Codeforces Round 9 -- B - Alice, Bob, Two Teams
- Codeforces 632B Alice, Bob, Two Teams 【水题】
- java.util.Properties类
- 修饰词的用法
- 简单了解Effective java
- 单链表一些总结
- Gym-100820 Racing Gems(二维LIS)
- Alice, Bob, Two Teams CodeForces
- mysql row模式的一些小问题(一)
- 浅拷贝、深拷贝与写时拷贝
- Integer源码(toHexString).md
- 构造代码块笔记
- ActiveMQ实战(三)--ActiveMQ的通信方式之主题发布订阅式(publish-subscribe)
- Codeforces 822C Hacker pack your bags! 区间
- 多态笔记
- android 网格布局