题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2277

Change the ball

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 694    Accepted Submission(s): 272

Problem Description
Garfield has three piles of balls, each pile has unique color of following: yellow, blue, and red. Now we also know Garfield has Y yellow balls, B blue balls, and R red balls. But Garfield just wants to change all the balls to one color. When he puts two balls of different color togather, the balls then change their colors automatically into the rest color. For instance, when Garfield puts a red one and a yellow one togather, the two balls immediately owns blue color, the same to other situations. But the rule doesn’t work when the two balls have the same color.
  Garfield is not able to estimate the minimal steps to achieve the aim. Can you tell him?
 
Input
For each line, there are three intergers Y, B, R(1<=Y,B,R<=1000),indicate the number refered above.
 
Output
For each case, tell Garfield the minimal steps to complete the assignment. If not, output the symbol “):”.
 
Sample Input
1 2 3
1 2 2

 
Sample Output
):
2
题目大意:有三种颜色的球(黄蓝黄),数量各不相同,现在给定每种球的个数,b并且有一种规则(拿起任意两种不同颜色的球他们的颜色会立马变成第三种颜色),问能否经过变换后所有的球都变成同一种颜色、需要的最少次数。
解题思路:能够达到目的的有两种情况:

①有两个球的数量相等(或者是三个都相等)

②有一种球是另一种球数量的三倍(每改变一次,有一个球会增加2个,另两个会各减少1个,中间差了3,也就是说,只有是三的倍数的情况,三个球才能实现完全转化),这种条件就需要分情况了,因为可能是都转化为球最多的那个,也有可能是都转化成球数不是最多的那个(最少次数就为球最多的球数(可以等价为这种球一直在向别的转化))

AC代码:

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 int main()
 5 {
 6     int a[4];
 7     while (~scanf ("%d %d %d",&a[1],&a[2],&a[3]))
 8     {
 9         sort (a+1,a+1+3);
10         if ( a[2] == a[1] || (a[2] - a[1]) % 3 ==0 )        //先看是否能把最少的转化成多的
11             printf ("%d\n",a[2]);       //如果可以的话,实际上是 a[2] 一直向 a[1]和 a[3]转化
12         else if ( a[2] == a[3] || (a[3] - a[2]) % 3 == 0 )
13             printf ("%d\n",a[3]);       //结合上一种,这一种也可以理解
14         else if ( a[1] == a[3] || (a[3] - a[1]) % 3 == 0 )
15             printf ("%d\n",a[3]);       //实际上,这种情况完全可以和上一种合并,但是为了程序的清楚就分开了
16         //else if ( a[2] == a[3] || (a[3] - a[2]) % 3 == 0 || a[1] == a[3] || (a[3] - a[1]) % 3 == 0 )
17         //  printf ("%d\n",a[3]);
18         //最后两个 else if 可以合并成这样
19         else
20             printf ("):\n");
21     }
22     return 0;
23 }  

转载于:https://www.cnblogs.com/yoke/p/5936665.html

HDU 2277 Change the ball相关推荐

  1. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  2. HDU 1556 Color the ball (数状数组)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. hdu 1556 Color the ball

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. 解题报告:hdu 1556 Color the ball(区间修改,单点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N ...

  5. HDU 1556 Color the ball

    题解:基础的树状数组区间修改,单点查询. #include <cstdio> #include <cstring> int c[100005],a,b,n; int modif ...

  6. HDU - 1556 Color the ball(树状数组)

    N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一 ...

  7. HDOJ/HDU 1556 Color the ball(树状数组)

    Problem Description N个气球排成一排,从左到右依次编号为1,2,3-.N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从 ...

  8. HDU 1556 Color the Ball 线段树 题解

    本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...

  9. hdu 1556 Color the ball 线段树 区间更新

    水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...

最新文章

  1. pyqt5 输入确认_对PyQt5的输入对话框使用(QInputDialog)详解
  2. ML之分类预测:机器学习中多分类预测数据集可视化(不同类别赋予不同颜色)设计思路及代码实现
  3. lower_bound和 upper_bound 用法(STL)
  4. Mysql 死锁过程及案例详解之记录锁与间隔锁Record Lock Gap Lock
  5. 阿里云混合云Apsara Stack 2.0发布,加速政企数智创新
  6. 分布式文件系统研究-fastDSF文件上传和下载流程
  7. flink 写kafka_网易云音乐基于 Flink + Kafka 的实时数仓建设实践
  8. 网络编程——常用协议解析
  9. 微信小程序 引入公共页面的几种情况
  10. 数据类型总结(干货)
  11. 一个基于特征向量的近似网页去重算法
  12. STS代码式预付费用电管理系统
  13. 数据库中间件DBLE学习(一) 基本介绍和快速搭建
  14. 英语3500词(14/20)dynasty主题 (2022.1.26)
  15. 深度爬取网易Lofter的爬虫
  16. atcod D - Staircase Sequences
  17. HTML开发过程中遇到的尺寸问题
  18. 关于echarts省份地图不显示
  19. 系统集成项目管理之项目成本管理(EV AC PV CV SV)
  20. 浏览器是直接加载二进制图片更快还是加载base64编码的图片更快?

热门文章

  1. [转载]Flex 2.0 实现SWF全屏
  2. 中交国通智能科技 招募 AI目标识别技术顾问
  3. 总奖池2.5万美元,Kaggle 新赛,单细胞分类与分割
  4. 科研人看过来!中国人工智能学会-华为MindSpore学术奖励基金发布通知
  5. 2020 COCO Keypoint Challenge 冠军之路!
  6. 【项目合作】基于Kinect人体模型重建与三围测量
  7. Github:深度学习文本检测识别(OCR)精选资源汇总
  8. python Scrapy爬取天气预报,零基础的你也可以快速上手
  9. java中怎么判断依赖关系,在Maven 2中,我如何知道依赖性来自哪个依赖?
  10. 计算机视觉论文-2021-07-09