【CodeForces - 632B】Alice, Bob, Two Teams (预处理,思维,前缀和后缀和)
題干:
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:
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 ABABAOutput
11Input
5 1 2 3 4 5 AAAAAOutput
15Input
1 1 BOutput
1Note
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兩個人玩游戲,給n個數,每個數對應一個 ' A ' 或 ' B ' , 現在B玩家可以選一個前綴或者一個后綴,把A變B,B變A,操作結束后,B玩家可以取走所有標號為 ' B?' 的數字,問B玩家可以取到的數字之和最大是多少??
解題報告:
? ? ?看題目名稱,猜是博弈?然而讓你失望了,這題和A沒有任何卵關系,,,先預處理出前綴A和B,后綴A和B,這四個數組。然后枚舉每一個元素,維護翻這一位或者不翻這一位的最大值maxx,并且維護和ans的最大值,輸出ans即可。
AC代碼:
#include<bits/stdc++.h> #define ll long long using namespace std; const int MAX = 5e5 + 5; ll val[MAX]; ll a[MAX],b[MAX],A[MAX],B[MAX]; ll ans,maxx; char s[MAX]; int main() {int n;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",val+i);cin>>s+1;//prefix預處理 for(int i = 1; i<=n; i++) {if(s[i] == 'A') {a[i] = a[i-1] + val[i];b[i] = b[i-1];}else {a[i] = a[i-1];b[i] = b[i-1] + val[i];}}//suffix預處理for(int i = n; i>=1; i--) {if(s[i] == 'A') {A[i] = A[i+1] + val[i];B[i] = B[i+1];}else {A[i] = A[i+1];B[i] = B[i+1] + val[i];}} for(int i = 1; i<=n; i++) {maxx = max(b[i]+B[i+1],a[i] + B[i+1]);ans = max(ans,maxx);}for(int i = n; i>=1; i--) {maxx = max(b[i] + B[i+1],b[i] + A[i+1]);ans = max(ans,maxx);}printf("%lld\n",ans);return 0 ; }?
總結
以上是生活随笔為你收集整理的【CodeForces - 632B】Alice, Bob, Two Teams (预处理,思维,前缀和后缀和)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 天价“修狗”?虚拟主播B站直播2小时收入
- 下一篇: 昔日安卓大哥HTC发布元宇宙新机Desi