D - Ball HDU - 4811(思维)

题目

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 10 9.
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
插入球的贡献值等于左边不同颜色的球的种类加上右边不同颜色球的种类,如果插在尾部头部相当于一边贡献为0。

解题思路

将球的规模减小来考虑,先考虑前提条件球的数量为I,I小于6的局面中的最优最小局面数量为j(j<=i),同时当球小于6时,可以发现如果某个球类数量大于2的效果对对应最优规模无贡献,所以最优最小问题转化为将每种球大于2时先当做2个,小于2时就是本身来考虑最优局面。
我们会发现在问题转化下,球的颜色和种类都没有影响贡献皆为1,插入一个新球,前面所有的球都可以产生贡献1,注意问题已经转化,球都不多于两个。
如此得到最优最小局面的贡献值为m= (1+n-1)(n-1)/2。(等差数列求和)然后多余的每个球插入得到的贡献都是n,因为n是最小局面最优的单次插入贡献值恰巧也是最小局面球的数量,与最小局面颜色无关,上一段有讲。
故答案为(n*(n-1)/2+(sum-n)*n),n为最小局面最优解。

#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
int main(){
	int a[3];
	int ans;
	ll n, sum;
	while(~scanf("%d %d %d", &a[0], &a[1], &a[2])){
		n = 0;
		sum = 0;
		for(int i = 0; i < 3; i++){
			if(a[i] > 2)
				n += 2;
			else
				n += a[i];
			sum += a[i];
		}	
		printf("%lld\n",(n*(n-1)/2+(sum-n)*n));
	}
	
	return 0;
}
发布了52 篇原创文章 · 获赞 2 · 访问量 853

猜你喜欢

转载自blog.csdn.net/qq_44714572/article/details/103944923