Alice, Bob, Two Teams
Time Limit:1500MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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
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
Hint
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,你能将该字符串分成两个串然后将任意一个子串中的A换成B,B换成A,每一个字符都有自己的价值,问怎样改动使串中B的价值最大,输出最大值;
思路:dp1[i] 表示[1,i]中的改变后B的价值,dp2[i] 表示[i,l] 改变B的价值,可以列出状态转移方程:当a[i]=A时dp1[i]=dp[i-1]+p[i],其余情况一样,数较大长整型数组内存占得太多,可用滚动数组;
失误:都是一些小问题:dp数组没用long long,sum没赋初值,int 和long long的转换,格式控制等。
代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=5e5+100;
int a[MAXN];
long long dp1[2],dp2[2];//wa点int
char str[MAXN];
long long Max(long long a,long long b)
{
return a>b?a:b;
}
int main()
{
int N,i,l;
while(~scanf("%d",&N))
{
for(i=1;i<=N;++i) scanf("%d",&a[i]);
scanf("%s",str+1);
long long suma=0,sumb=0;//没有赋值做运算
l=strlen(str+1); //没加1
for(i=1;str[i]!='\0';++i)
{
if(str[i]=='B') suma+=a[i];
else sumb+=a[i];
}
dp1[0]=suma; dp2[0]=sumb;
long long ans=Max(suma,sumb);//一个字符都没改的情况
for(i=1;i<=l;++i)
{
if(str[i]=='A')//应该不用滚动数组也能过
{
dp1[i%2]=dp1[(i-1)%2]+a[i];
dp2[i%2]=dp2[(i-1)%2]-a[i];
}
if(str[i]=='B')
{
dp1[i%2]=dp1[(i-1)%2]-a[i];
dp2[i%2]=dp2[(i-1)%2]+a[i];
}
ans=Max(ans,Max(dp1[i%2],dp2[i%2]));
}
printf("%lld\n",ans);
}
return 0;
}