kind of mathematical puzzle

187 Views Asked by At

i was recently doing this problem---

problem statement

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·109) — 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.

test case:

5 4 3

answer:

4

the problem is pure mathematical but after trying it for hours i am not able to figure correct solution,please help!

this approach fails*(i am following a greedy approach and taking 2 ballons from the element with greater value)*

 ll pro;

    while(1)
    {
        pro=a[2]/2;

        a[2]=a[2]-pro*2;

        if(pro>a[1])
            {sum+=a[1];a[1]=0;break;}
        else
        {
            sum+=pro;a[1]=a[1]-pro;
            if(a[1]>a[2])
                swap(a[1],a[2]);
            if(a[1]==0||a[2]==0)
                break;
            if(a[1]==1&&a[2]==1)

                break;
        }
    }
    cout<<sum<<endl;
1

There are 1 best solutions below

0
On BEST ANSWER

I think the greedy strategy should work. Here is the rough idea:

In every loop compare the currently remaining number of red, green and blue balloons (denote by $r_x, g_x$ and $b_x$ respectively). WLOG assume $r_x > g_x > b_x$. Then use $2$ red balloons and $1$ green balloon. Subtract the used balloons from each color and then loop repeatedly.

And mathematically, WLOG, assume $r \ge g \ge b$. The maximum number of tables should be $\lfloor (r+g+b)/3 \rfloor$ if $r \le 2 \times (g + b)$. But if $r > 2 \times (g + b)$, you can at most decorate $(g + b)$ tables (by using $2$ red balloons and $1$ balloon of another color for each table).