Codeforces Round #233 (Div. 2) B. Red and Blue Balls
2014-03-02 01:43
441 查看
B. Red and Blue Balls
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 %I64dspecifier.
Sample test(s)
input
output
input
output
input
output
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.
先模拟,超时了,然后根据模拟的结果来找规律,发现之和B的位置有关,并且成2的次方关系
若B在I位置,则cnt += pow(2,i)即可;
代码如下:
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 %I64dspecifier.
Sample test(s)
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.
先模拟,超时了,然后根据模拟的结果来找规律,发现之和B的位置有关,并且成2的次方关系
若B在I位置,则cnt += pow(2,i)即可;
代码如下:
#include <algorithm> #include <iostream> #include <cmath> #include <string> #include <vector> #include <string.h> #define MAXN 100 #define ll long long using namespace std; int n; char str[MAXN]; int main(void){ while(cin >> n){ cin >> str; ll cnt = 0; for(int i=0; i<n; ++i){ if(str[i] == 'B') cnt += pow(int(2),(i)); } cout << cnt << endl; } return 0; }
相关文章推荐
- codeforces#233_div2_B Red and Blue Balls
- codeforces#233_div2_B Red and Blue Balls
- Codeforces Round #233 (Div. 2) B. Red and Blue Balls
- cf B Red and Blue Balls
- AC日记——Red and Blue Balls codeforces 399b
- 【CodeForces 339B】Red and Blue Balls 找规律
- Codeforces Round #158 (Div. 2) C. Balls and Boxes 模拟
- 【AGC014E】Blue and Red Tree 并查集 启发式合并
- Codeforces Round #309 (Div. 1) A Kyoya and Colored Balls
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A. Bear and Three Balls 水题
- CodeForces 653 A. Bear and Three Balls——(IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2))
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2)-A - Bear and Three Balls-水题
- 【AGC014】E - Blue and Red Tree
- Red/Blue Spanning Tree----HDU_4263----并查集and生成树
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls
- codeforces IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A. Bear and Three Balls
- pku3715 Blue and Red
- CF#309-DIV2-C. Kyoya and Colored Balls-组合数学
- Codeforces Round #158 (Div. 2)C. Balls and Boxes(模拟)
- poj 3715 Blue and Red(二分图最大匹配+字典序输出)