hihocoder-1135-Magic Box

三种颜色的球一次放入盒子,当数量差满足x,y,z时盒子中的球的个数清零,求盒子中出现数量最多的球的个数

数据范围:球的数量<=20000

分析:直接跟踪一遍放入球的过程,判断是否满足x,y,z,并更新答案

注意:在for循环中是当满足条件是更新答案,退出循环后还要更新一遍答案,这是最后一轮的盒子中剩下的球的数量

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<cmath>
 5 #include<vector>
 6 #include<algorithm>
 7 using namespace std;
 8
 9 int x,y,z;
10 string s;
11
12 bool chk(int a,int b,int c)
13 {
14     int u=abs(a-b);
15     int v=abs(a-c);
16     int t=abs(b-c);
17     if(u==x&&v==y&&t==z) return true;
18     if(u==x&&v==z&&t==y) return true;
19     if(u==y&&v==x&&t==z) return true;
20     if(u==y&&v==z&&t==x) return true;
21     if(u==z&&v==x&&t==y) return true;
22     if(u==z&&v==y&&t==x) return true;
23     return false;
24 }
25
26 int main()
27 {
28     while(cin>>x>>y>>z){
29         cin>>s;
30         int cr=0,cb=0,cy=0;
31         int sum=0;
32         int ans=0;
33         for(int i=0;i<s.size();i++){
34             sum++;
35             if(s[i]==‘R‘) cr++;
36             else if(s[i]==‘B‘) cb++;
37             else cy++;
38             if(chk(cr,cb,cy)){
39                 ans=max(ans,sum);
40                 sum=0;
41                 cr=cb=cy=0;
42             }
43         }
44         ans=max(ans,sum);
45         cout<<ans<<endl;
46     }
47 }
时间: 2024-08-26 03:49:51

hihocoder-1135-Magic Box的相关文章

[hihocoder] Magic Box

题目1 : 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

hihoCoder#1135

刚开始学习C语言,准备在做hiho的题目的过程中来学习,在此进行记录,如果代码中有错误或者不当的地方还请指正. 时间限制: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)

hdu--5155Harry And Magic Box(组合数+容斥原理)

Harry And Magic Box Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2015-01-07) Description One day, Harry got a magical box. The box is made of n*m grids. There are sp

微软线上笔试-2015-4-3(1,2题) Magic Box &amp;&amp; Professor Q&#39;s Software

写在前面: http://blog.csdn.net/michael_kong_nju/article/details/44872519 关于4.3号的微软线上挑战赛,感觉自己还是刷题刷少了,表现在几个方面:1. 编程经验不足.2. 算法的使用不灵活.所以下面还是要加强OJ的训练, 把Leetcode上的题多做做.后面又把4道题仔细的编写调试了一下,希望和我情况类似的同学也能加紧代码的训练. 1. 第一题的原题是: The circus clown Sunny has a magic box.

hdu5155---Harry And Magic Box

Harry And Magic Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 131    Accepted Submission(s): 64 Problem Description One day, Harry got a magical box. The box is made of n*m grids. There a

HDOJ 5155 Harry And Magic Box DP

dp[i][j] 表示 长宽为i,j的矩形的可能的总数 dp[i][j+1] 可由 dp[i][j] 推过来,枚举dp[i][j]所保留的行数(1...i)即可 Harry And Magic Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 441    Accepted Submission(s): 209 Problem D

BestCoder Round #25 1002 Harry And Magic Box [dp]

传送门 Harry And Magic Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 165    Accepted Submission(s): 64 Problem Description One day, Harry got a magical box. The box is made of n*m grids. Ther

HDU 5147 Harry And Magic Box dp+组合数

点击打开链接 Harry And Magic Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 197    Accepted Submission(s): 97 Problem Description One day, Harry got a magical box. The box is made of n*m grids.

HDU 5155 Harry And Magic Box(组合数学+容斥)

Problem Description One day, Harry got a magical box. The box is made of n*m grids. There are sparking jewel in some grids. But the top and bottom of the box is locked by amazing magic, so Harry can't see the inside from the top or bottom. However, f

HDU 5155 Harry And Magic Box --DP

题意:nxm的棋盘,要求每行每列至少放一个棋子的方法数. 解法:首先可以明确是DP,这种行和列的DP很多时候都要一行一行的推过去,即至少枚举此行和前一行. dp[i][j]表示前 i 行有 j 列都有了棋子,且每行也有棋子. 这题做法: 从第1行到第n行,枚举这一行有k列已至少有一个,再枚举前一行有j列至少有一个,然后枚举这一行新放多少个棋子t,至少一个(因为每行至少一个) 那么有 dp[i][k] += dp[i-1][j]*C[m-j][k-j]*C[j][t-(k-j)], C表示组合数