「CF399B」Red and Blue Balls
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.
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.
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.
1 2 |
3 RBR |
1 |
2 |
1 2 |
4 RBBR |
1 |
6 |
1 2 |
5 RBBRR |
1 |
6 |
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 second example is depicted below. The blue arrow denotes a single operation.
看完就开始打模拟,数据范围这么小是吧。。然后果断T了
原来是找规律啊。。
把第n个蓝球变成红球要2^n次操作。。然后累加起来,开个longlong
模拟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define inf 0x7fffffff using namespace std; int n,st[51],top;char a[51]; bool work() { while(st[top]==1&&top>0)top--; if(top==0)return 0; if(st[top]==2)st[top]=1; for(int i=top+1;i<=n;i++)st[i]=2; return 1; } int main() { scanf("%d",&n); scanf("%s",a); for(int i=1;i<=n;i++) if(a[i-1]=='R')st[n-i+1]=1; else st[n-i+1]=2; for(int i=1;;i++) {top=n;if(!work()){printf("%d",i-1);break;}} return 0; } |
正解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define inf 0x7fffffff using namespace std; int n,st[51];char a[51]; long long ans; long long pow(int x) { long long s=1; for(int i=1;i<x;i++) s*=2; return s; } int main() { scanf("%d",&n); scanf("%s",a); for(int i=1;i<=n;i++) if(a[i-1]=='R')st[i]=1; else st[i]=2; for(int i=1;i<=n;i++)if(st[i]==2)ans+=pow(i); printf("%lld",ans); return 0; } |