A - Irreversible operation
AtCoder Grand Contest 029:https://atcoder.jp/contests/agc029/tasks/agc029_a
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 300300 points
Problem Statement
There are N Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.) The state of each piece is represented by a string S of length N. If Si=B
, the ii-th piece from the left is showing black; If Si=W
, the i-th piece from the left is showing white.
Consider performing the following operation:
- Choose i (1≤i<N) such that the i-th piece from the left is showing black and the (i+1)-th piece from the left is showing white, then flip both of those pieces. That is, the i-th piece from the left is now showing white and the (i+1)-th piece from the left is now showing black.
Find the maximum possible number of times this operation can be performed.
Constraints
- 1≤|S|≤2×10^5
- Si = 'B' or 'W'
Input
Input is given from Standard Input in the following format:
S
Output
Print the maximum possible number of times the operation can be performed.
Sample Input 1
BBW
Sample Output 1
2
The operation can be performed twice, as follows:
- Flip the second and third pieces from the left.
- Flip the first and second pieces from the left.
Sample Input 2
BWBWBW
Sample Output 2
6
题解:
代码:
#include <iostream>
using namespace std;
typedef long long LL;
int main()
{
string s;
cin>>s;
LL n=s.length(),sum=0;
if(n==1)sum=0;
else{
LL pre=0;
for(LL i=0;i<n;i++){
if(s[i]=='B')pre++;
else sum+=pre;
}
}
cout<<sum<<endl;
return 0;
}