HDU 4811 Ball 贪心
题目链接:
题目
Ball
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
问题描述
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 109.
输出
For each test case, print the answer in one line.
样例
input
2 2 2
3 3 3
4 4 4
output
15
33
51
题意
给你三种颜色的气球各若干个。现在把所有的气球放成一排,放第一个没有贡献值,之后每放一个气球,贡献值等于放在这个气球左边的不同颜色种类数加上放在这个气球右边的不同颜色种类数。最后要求一种摆放方案使得所有贡献值的总和的最大值。
题解
各种分类讨论的挫代码orz:
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
int main() {
int tc,kase=0;
LL a[22];
while(scanf("%lld%lld%lld",&a[0],&a[1],&a[2])==3) {
sort(a,a+3);
if(a[0]>=2&&a[1]>=2&&a[2]>=2) {
LL ans=15+6*(a[0]+a[1]+a[2]-6);
printf("%lld\n",ans);
} else if(a[0]>=1&&a[1]>=1&&a[2]>=1){
a[0]--; a[1]--; a[2]--;
LL ans=3;
if(a[1]>=1&&a[2]>=1){
ans+=7+5*(a[1]+a[2]-2);
}else if(a[2]>0){
ans+=3+(a[2]-1)*4;
}
printf("%lld\n",ans);
}else{
LL ans=0;
if(a[1]>=2&&a[2]>=2){
ans=6+(a[1]+a[2]-4)*4;
}else if(a[1]>=1&&a[2]>=1){
ans=1;
a[1]--; a[2]--;
if(a[2]>0){
ans+=2+(a[2]-1)*3;
}
}else{
a[2]--;
if(a[2]>0){
ans=1+2*(a[2]-1);
}else{
ans=0;
}
}
printf("%lld\n",ans);
}
}
return 0;
}
优化之后的:(这很优化)
首先,每种颜色取出两个(不足两个的有几个选几个),易知先取出来的这几个数可以取到最好的情况(一个等差数列),之后我们采取贪心的策略在中间放,同样也是最优的。 假设我们能先取出x个(x<=6),那我们形成的最优序列就是:1 2 3 4 5 5 5 5 ... 5; 答案就是:x(x-1)/2+x(sum-x);
#include<stdio.h>
#include<algorithm>
typedef long long LL;
int main(){
int a[3];
while(scanf("%d%d%d",&a[0],&a[1],&a[2])==3){
LL cnt=0;
for(int i=0;i<3;i++) cnt+=std::min(2,a[i]);
cnt=cnt*(cnt-1)/2+cnt*((LL)a[0]+a[1]+a[2]-cnt);
printf("%lld\n",cnt);
}
return 0;
}