CF——399div2.B.Red and Blue Balls

3 篇文章 0 订阅

B. Red and Blue Balls

time limit per test0.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
User ainta has a stack of n red and blue balls. He can apply a certain operation which changes the colors of the balls inside the stack.

While the top ball inside the stack is red, pop the ball from the top of the stack.
Then replace the blue ball on the top with a red ball.
And finally push some blue balls to the stack until the stack has total of n balls inside.

If there are no blue balls inside the stack, ainta can’t apply this operation. Given the initial state of the stack, ainta wants to know the maximum number of operations he can repeatedly apply.

Input
The first line contains an integer n (1 ≤ n ≤ 50) — the number of balls inside the stack.

The second line contains a string s (|s| = n) describing the initial state of the stack. The i-th character of the string s denotes the color of the i-th ball (we’ll number the balls from top to bottom of the stack). If the character is “R”, the color is red. If the character is “B”, the color is blue.

Output
Print the maximum number of operations ainta can repeatedly apply.

Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
input
3
RBR
output
2
input
4
RBBR
output
6
input
5
RBBRR
output
6

Note
The first example is depicted below.

The explanation how user ainta applies the first operation. He pops out one red ball, changes the color of the ball in the middle from blue to red, and pushes one blue ball.
在这里插入图片描述

The explanation how user ainta applies the second operation. He will not pop out red balls, he simply changes the color of the ball on the top from blue to red.
在这里插入图片描述

From now on, ainta can’t apply any operation because there are no blue balls inside the stack. ainta applied two operations, so the answer is 2.
在这里插入图片描述
The second example is depicted below. The blue arrow denotes a single operation.
PS: 这道题看起来感觉要模拟,但是仔细读题与打表发现,这就是一个只关于蓝球二进制的用法,就计算蓝色球的二进制表示在十进制下的数就行了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	int n;
	ll sum;
	char a[55];
	while(cin>>n)
	{
		sum=0;
		cin>>a;
		for(int i=0;i<n;i++){
			if(a[i]=='B')
			sum+=pow(2,i);
		}
		cout<<sum<<endl;
	}
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值