hihoCoder 1135-Magic Box

hihoCoder 1135-Magic Box #1135 : Magic Box 时间限制: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
简单逻辑题,每增加一个字母更新box中球的数量,并判断是否可以vanish。 完整代码如下: [cpp] #include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<algorithm> using namespace std; int num_r=0,num_y=0,num_b=0,ans=0; int xyz[3],diff[3]; string line; void Work() { ans=max(ans,num_r+num_y+num_b); diff[0]=abs(num_r-num_y); diff[1]=abs(num_r-num_b); diff[2]=abs(num_y-num_b); sort(diff,diff+3); if(diff[0]==xyz[0]&&diff[1]==xyz[1]&&diff[2]==xyz[2]) { num_r=0; num_y=0; num_b=0; } } int main() { cin>>xyz[0]>>xyz[1]>>xyz[2]>>line; sort(xyz,xyz+3); for(int i=0;i<line.size();i++) { switch(line[i]) { case ‘R’: num_r++; break; case ‘Y’: num_y++; break; case ‘B’: num_b++; break; } Work(); } printf("%d\n",ans); return 0; } [/cpp] 本代码提交AC,用时5MS,内存0MB。]]>

Leave a Reply

Your email address will not be published. Required fields are marked *