您的位置:首页 > 其它

HDU 4811 Ball(公式)

2016-11-09 09:30 330 查看

Ball

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2317    Accepted Submission(s): 968

[align=left]Problem Description[/align]
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?
 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
For each test case, print the answer in one line.
 

[align=left]Sample Input[/align]

2 2 2
3 3 3
4 4 4

 

[align=left]Sample Output[/align]

15
33
51

 

[align=left]Source[/align]
2013ACM/ICPC亚洲区南京站现场赛——题目重现
 

题意:需要将R\Y\B三种颜色的球排成一排,每次可以选择将一个球插在排好的球中间,此时得分为该球前面球的不同颜色数+后面球的不同颜色数,也可以选择将球放在最后,此时得分为该球前面的球的不同颜色数,给定三种颜色球个数,求最大得分。
思路:直接推公式,每次用贪心的方法放保证其得分最大,但是分类讨论的时候一定要全面,不能有遗漏,且中间需要用long long存,否则会爆。

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;

int main(){

long long a[3];
while(~scanf("%lld %lld %lld",&a[0],&a[1],&a[2])){
sort(a,a+3);
long long sum=a[0]+a[1]+a[2];
long long ans=0;
if(!a[0]&&!a[1]){
if(a[2]==0){
puts("0");
}
if(a[2]==1){
puts("0");
}
if(a[2]>=2){
ans=1+2*(a[2]-2);
printf("%lld\n",ans);
}
}
if(!a[0]){
if(a[1]==1&&a[2]==1){
puts("1");
}
if(a[1]==1&&a[2]>=2){
ans=3+3*(a[2]-2);
printf("%lld\n",ans);

}
if(a[1]>=2&&a[2]>=2){
ans=6+4*(a[1]+a[2]-4);
printf("%lld\n",ans);
}
}
if(a[0]){
if(a[0]==1&&a[1]==1&&a[2]==1){
puts("3");
}
if(a[0]==1&&a[1]==1&&a[2]>=2){
ans=6+4*(a[2]-2);
printf("%lld\n",ans);
}
if(a[0]==1&&a[1]>=2&&a[2]>=2){
ans=10+5*(a[1]+a[2]-4);
printf("%lld\n",ans);
}
if(a[0]>=2){
ans=15+6*(sum-6);
printf("%lld\n",ans);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: