题目描述:
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?
输入描述:
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.
输出描述:
For each test case, print the answer in one line.
输入:
2 2 2
3 3 3
4 4 4
输出:
15
33
51
题意:
这些球可以是三种颜色中的一种:红、黄、蓝。更准确地说,R个红色的球,Y个黄色的球和B个蓝色的球。他可以把这些球一个接一个地按任何顺序放在桌上。每次把一个新球放在桌子上,他可能会把它插在已经摆放好的一排球的中间(或一端)。
此外,每次把球放在桌上,他都会得到一些分数(可能是零分)。点数计算方法如下:
1.第一个球放在桌上,他得了0分。
2.如果他把球放在一行的一端,他得到的分数等于桌子上已经放置的球的不同颜色的数量。
3.如果他把球放在两个球之间,他得到的分数等于当前放置的球之前不同颜色的球的数量,加上当前放置的球之后不同颜色的球的数量。
把球放在桌子上所能得到的最高分数是多少?
题解:
找规律,推导出公式,因为只有三种颜色,如果每种颜色都有2的及以上,那么可以先在两边各摆三种颜色的球,这样每次把其他球放入中间时都能得到6分,即ans=(R-2+Y-2+B-2)*6+15(15为在两边各摆三种颜色的球的过程所获得的总得分)。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
ll judge(ll x){
if(x < 2) return x;
else return 2;
}
int main(){
ll r,y,b;
while(scanf("%lld%lld%lld",&r,&y,&b)!=EOF){
ll val = judge(r) + judge(y) + judge(b);
ll sum = r + y + b;
ll ans = 0;
for(int i = 0; i < val; i ++){
ans += i;
}
if(sum > val){
ans += (sum - val) * val;
}
printf("%lld\n",ans);
}
return 0;
}