1. 程式人生 > >hdu4811-Ball(2013ACM/ICPC亞洲區南京站現場賽)

hdu4811-Ball(2013ACM/ICPC亞洲區南京站現場賽)

namespace rate colors cin 三種 ase ron red arr

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4811

題目描述:

Problem Description Jenny likes balls. He has some balls and he wants to arrange them in a row on the table. Each of those balls can be one of three possible colors: red, yellow, or blue. More precisely, Jenny has R red balls, Y yellow balls and B blue balls. He may put these balls in any order on the table, one after another. Each time Jenny places a new ball on the table, he may insert it somewhere in the middle (or at one end) of the already-placed row of balls. Additionally, each time Jenny places a ball on the table, he scores some points (possibly zero). The number of points is calculated as follows: 1.For the first ball being placed on the table, he scores 0 point. 2.If he places the ball at one end of the row, the number of points he scores equals to the number of different colors of the already-placed balls (i.e. expect the current one) on the table. 3.If he places the ball between two balls, the number of points he scores equals to the number of different colors of the balls before the currently placed ball, plus the number of different colors of the balls after the current one. What‘s the maximal total number of points that Jenny can earn by placing the balls on the table? Input
There are several test cases, please process till EOF. Each test case contains only one line with 3 integers R, Y and B, separated by single spaces. All numbers in input are non-negative and won‘t exceed 109. Output For each test case, print the answer in one line. Sample Input 2 2 2 3 3 3 4 4 4 Sample Output
15 33 51

題意:

有三種顏色,給你每種顏色的球的數量,有以下兩種得分方式,問你如何放置這些球,讓總得分最大。

方式一:放第一個球的得分為0

方式二:放在最後面的得分為之前的所有球的顏色種數

方式三:放在中間的得分為左邊球的顏色種數+右邊球的顏色種數

思路:

找規律,推導出公式,因為只有三種顏色,如果每種顏色都有2的及以上,那麽可以先在兩邊各擺三種顏色的球,這樣每次把其他球放入中間時都能得到6分,即ans=(R-2+Y-2+B-2)*6+15(15為在兩邊各擺三種顏色的球的過程所獲得的總得分)。

其實如果一個球的數量超過了2,那麽剩下的就是一個乘法了。 這個理解很簡單,因為超過了2的話,說明最優的方案一定是左右各一個,不然如果都在一邊的話就只得1分了。

代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;

ll a[3][3][3];//a[i][j][k]表示紅色球為i個,黃色球為j個,藍色球為k個的排列總數
ll f[4],n,k,tep;

int main()
{
    a[0][0][0]=0,a[0][0][1]=0,a[0][0][2]=1,a[0][1][1]=1,a[0][1][2]=3,a[0][2][2]=6;
    a[1][1][1]=3,a[1][1][2]=6,a[1][2][2]=10,a[2][2][2]=15;
    while (cin>>f[1]>>f[2]>>f[3])
    {
        n=k=0;
        for (int i=1; i<4; i++)
        {
            if (f[i]>2) k=2;
                else k=f[i];
            n+=f[i]-k,f[i]=k;
        }
        sort(f+1,f+4);
        tep=f[1]+f[2]+f[3];
        cout<<a[f[1]][f[2]][f[3]]+n*tep<<endl;
    }

    return 0;
}

hdu4811-Ball(2013ACM/ICPC亞洲區南京站現場賽)