hihoCoder week 80

P1 : Magic Box

Time Limit: 10000ms
Case Time Limit: 1000ms
Memory Limit: 256MB

Description

The circus clown Sunny has a magic box. When the circus is performing, Sunny puts some balls into the box one by one. The balls are in three colors: red(R), yellow(Y) and blue(B). Let Cr, Cy, Cb denote the numbers of red, yellow, blue balls in the box. Whenever the differences among Cr, Cy, Cb happen to be x, y, z, all balls in the box vanish. Given x, y, z and the sequence in which Sunny put the balls, you are to find what is the maximum number of balls in the box ever.

For example, let's assume x=1, y=2, z=3 and the sequence is RRYBRBRYBRY. After Sunny puts the first 7 balls, RRYBRBR, into the box, Cr, Cy, Cb are 4, 1, 2 respectively. The differences are exactly 1, 2, 3. (|Cr-Cy|=3, |Cy-Cb|=1, |Cb-Cr|=2) Then all the 7 balls vanish. Finally there are 4 balls in the box, after Sunny puts the remaining balls. So the box contains 7 balls at most, after Sunny puts the first 7 balls and before they vanish.

Input

Line 1: x y z

Line 2: the sequence consisting of only three characters 'R', 'Y' and 'B'.

For 30% data, the length of the sequence is no more than 200.

For 100% data, the length of the sequence is no more than 20,000, 0 <= x, y, z <= 20.

Output

The maximum number of balls in the box ever.

Hint

Another Sample

Sample Input Sample Output
0 0 0
RBYRRBY            
4


Sample Input
1 2 3
RRYBRBRYBRY
Sample Output
7

题意:向盒子里面按给定序列依次放球,R表示红色的球, Y表示黄色的球, B表示蓝色的球。分别用cr、cy、cb计数,记a = |cr - cy|, b = |cy - cb|, c = |cr - cb|,若a、b、c与x、y、z存在对应相等(不一定是a == x),那么盒子里面的球都会消失,问你盒子里最多可能存在多少个球。



模拟,维护最大值就可以了。


AC代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (20000+10)
#define MAXM (200000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
using namespace std;
char str[MAXN];
int x[3], xx[3];
bool judge(int a, int b, int c)
{
    xx[0] = abs(a-b); xx[1] = abs(b-c); xx[2] = abs(a-c);
    sort(xx, xx+3);
    return x[0] == xx[0] && x[1] == xx[1] && x[2] == xx[2];
}
int main()
{
    while(scanf("%d%d%d", &x[0], &x[1], &x[2]) != EOF)
    {
        Rs(str); int len = strlen(str);
        int cr = 0, cy = 0, cb = 0;
        int ans = 0; int last = -1;
        sort(x, x+3);
        for(int i = 0; i < len; i++)
        {
            cr += str[i] == 'R';
            cy += str[i] == 'Y';
            cb += str[i] == 'B';
            ans = max(ans, i - last);
            if(judge(cr, cy, cb))
            {
                last = i;
                cr = cy = cb = 0;
            }
        }
        Pi(ans);
    }
    return 0;
}


你可能感兴趣的:(模拟)