/**
*@author StormMaybin
*@Date 2016-08-16
*/
生命不息,奋斗不止!
大概十几天二十天没有更博客了,这段时间参加了算法集训,参加算法集训主要的目的是为了了解一些常用的算法,提高代码能力等等,并不是单纯为了参加算法竞赛,作为一个有理想的新时代的码农,不懂算法怎么说得过去(哈哈,个人意见)!这段时间刷了不少的题,接下来这段时间就来用博客记录这样一个有意义的经历并且来复习一下所学的东西!
今天是算法集训总结的第一天,先来水几道题来压压惊(哈哈),按照我们教练的意思就是锻炼一下代码能力!
题目描述
You have r red, g green and b blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn’t have the same color. What maximum number t of tables can be decorated if we know number of balloons of each color?
Your task is to write a program that for given values r, g and b will find the maximum number t of tables, that can be decorated in the required manner.
input
The single line contains three integers r, g and b (0 ≤ r, g, b ≤ 2·10^9) — the number of red, green and blue baloons respectively. The numbers are separated by exactly one space.
output
Print a single integer t — the maximum number of tables that can be decorated in the required manner.
Examples
Input
5 4 3
Output
4
Input
1 1 1
Output
1
Input
2 3 3
Output
2
题目意思很简单,就是给你红色、绿色、蓝色的气球若干个,要你拿这些气球去装饰桌子,装饰桌子的三个气球不能是完全相同的(不能是同一个颜色的)问你做多可以装饰多少个桌子!
这个题有点类似贪心策略,现在我们先拿贪心来做一下试试,试想一下,如果是给我们两种颜色的话,让我们用两种不同的颜色的三个气球来装饰桌子,我们怎么去考虑?这就用到了贪心的思想,我们让尽可能多的气球先开始每次拿两个,我们来举个例子说明一下,比如两种颜色是r和g比如r < g,那么我们开始应该先这样装饰原因很简单,如果你开始是的话,这样g还剩下很多,r不够用了 ,很明显不是我们想要的,那么我们只能采取第一种策略,知道r和g相等的时候我们开始换着来(怎么换着来呢,其实就是穿插着,比如先开始是那么下次我们取)这样两次用的r和g的数量是一样的,类似的如果是三种气球的话,我们可以模仿两个气球的思路,比如r g b关系是r < g < b,现在我们手里的b气球是最多的,那么我们一定要每次拿2个b吗?其实不对,我们要比较一下(r + g) * 2 和 b的大小,如果是(r + g) * 2 <= b的话,我们每次必须拿两个b,r和g随便拿一个,如果(r + g) * 2 > b的话,我们只能采取上述的策略来减少b的数量,其实最后的结果就是(r + g + b) / 3;
package com.stormma.day1;
import java.util.Arrays;
import java.util.Scanner;
public class MainA2
{
Scanner scan = new Scanner (System.in);
public MainA2()
{
while (scan.hasNext())
{
long [] mp = new long [3];
for (int i = 0; i < 3; i++)
mp[i] = scan.nextLong();
Arrays.sort(mp);
//现在开始mp[0]是最少的,mp[2]是最大的
if ((mp[0] + mp[1]) * 2 <= mp[2])
System.out.println(mp[0]+mp[1]);
else
System.out.println((mp[0]+mp[1]+mp[2]) / 3);
}
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
new MainA2();
}
}
我们知道r g b这三个颜色最多有(r + g + b) / 3个方案,通过模拟一下我们可以发现每次的结果其实就是{(r + g + b) / 3, r+g, r+b, g+b}中的最小值
package com.stormma.day;
import java.util.Scanner;
public class MainA
{
Scanner scan = new Scanner(System.in);
public MainA()
{
while (scan.hasNext())
{
long r = scan.nextLong();
long g = scan.nextLong();
long b = scan.nextLong();
Long ans = Math.min((r+g+b)/3, Math.min(r+g, Math.min(g+b, r+b)));
System.out.println(ans);
}
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
new MainA();
}
}
这有个坑就是一定要读入long ,因为我们输出的有个(r + g + b)/3,r+g+b的值有可能超出int的范围!
这道题是不是很水,今天是个开始,接下来的日子会有难度不低的题目(对我来说难度不低,我太渣哈哈)!