hihoCoder week 80

本文介绍了一个有趣的算法问题——魔法盒中的球。任务是在给定颜色球的放入顺序及特定条件时,找出盒中球的最大数量。文章通过示例详细解析了问题背景与解决思路,并提供了完整的AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 InputSample 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 <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值