hdu4811-Ball(2013ACM/ICPC亚洲区南京站现场赛)
题目链接: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; }