【hihoCoder题解】 1135

作者: admin 日期: 2016-03-25 14:31:39 人气: - 评论: 0
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
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.

输入
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.

输出
The maximum number of balls in the box ever.

提示
Another Sample

Sample Input	Sample Output
0 0 0
RBYRRBY            	4





样例输入
1 2 3
RRYBRBRYBRY
样例输出
7

题目大意:
马戏团的小丑Sunny有一个魔法盒子,在马戏表演的时候,Sunny将一些球放入盒子里。
这些球有三总颜色:红(R),黄(Y),蓝(B),设CR,CY,CB表示红色、黄色、蓝色的球在盒子里的数量。
当CR,CY,CB之间的差值恰好等于x,y,z的时候盒子里面所有的球消失,

基于x, y, z和Sunny放入球的顺序,请你找到盒子里面能放入最多的球的数量

#include <iostream>
#include <cstring>
#include <string>
#include <set>
#include <map>
using namespace std;
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
void sort(int *a,int *b,int *c){
if (*a > *b){
swap(a, b);
}
if (*b > *c){
swap(b, c);
}
if (*a > *b){
swap(a, b);
}
}
int abs(int x){
if (x < 0){
return -x;
}
else{
return x;
}
}
void dx(int *d1,int *d2,int *d3,int cr,int cy,int cb){
*d1 = abs(cr - cy);
*d2 = abs(cy - cb);
*d3 = abs(cb - cr);
}
bool cmp(int d1,int d2,int d3,int x,int y,int z){
sort(&d1, &d2, &d3);
if (d1 == x && d2 == y && d3 == z){
return true;
}
return false;
}
int main()
{
int x, y, z;
int cr=0, cy=0, cb=0;
int d1, d2, d3;
int max = 0;
string s;
cin >> x >> y >> z;
sort(&x, &y, &z);
cin >> s;
for (string::iterator i = s.begin(); i < s.end(); i++){
if (*i == 'R'){
cr++;
}
else if (*i == 'Y'){
cy++;
}
else{
cb++;
}
dx(&d1, &d2, &d3, cr, cy, cb);
int sum = cr + cy + cb;
if (sum > max){
max = sum;
}
if (cmp(d1, d2, d3, x, y, z)){
cr = 0, cy = 0, cb = 0;
}
}
cout << max<<endl;
return 0;
}


代码地址:https://github.com/iptop/hihoCoderProblemSolving/blob/master/1135/main.cpp

相关内容

发表评论
更多 网友评论0 条评论)
暂无评论

Copyright © 2012-2014 我的代码板 Inc. 保留所有权利。

页面耗时0.0297秒, 内存占用1.95 MB, 访问数据库13次

闽ICP备15009223号-1