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
题意:
 
给出三种球的个数,然后再求题目要求的那个最大值;现在把所有的气球放成一排,放第一个没有贡献值,之后每放一个气球,贡献值等于放在这个气球左边的不同颜色种类数加上放在这个气球右边的不同颜色种类数。最后要求一种摆放方案使得所有贡献值的总和的最大值。
 
思路:
 
分情况讨论啦,比如现在现在三种球的个数都大于2,那么在放球的话就可以最大得到6的分数了,然后其他的情况也是这样啦;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
const int maxn=1e5+5;
const int INF=0x3f3f3f3f;

int main()
{
    int a,b,c;
    ll sum=0,ans=0;
    while(cin>>a>>b>>c)
    {
        sum=a+b+c;
        if(sum==0) ans=0;
        if(sum==1) ans=0;
        if(sum==2) ans=1;
        if(sum==3) ans=3;
        if(a>=2 && b>=2 && c>=2) ans=15+(sum-6)*6;  ///2 2 2
        int flag=0;
        if((a==1 && b>=2 && c>=2) || (b==1 && c>=2 && a>=2) || (c==1 && b>=2 && a>=2)) flag=1;
        if(flag==1) ans=10+(sum-5)*5;    ///2 2 1
        if((a==1 && b==1 && c>=2) || (b==1 && c==1 && a>=2) || (c==1 && a==1 && b>=2)) flag=2;
        if(flag==2) ans=6+(sum-4)*4;     ///2 1 1
        if((a==0 && b>=2 && c>=2) || (b==0 && c>=2 && a>=2) || (c==0 && b>=2 && a>=2)) flag=3;
        if(flag==3) ans=6+(sum-4)*4;     ///2 2 0
        if((a==0 && b==1 && c>=2) || (a==0 && c==1 && b>=2) || (a==1 && b==0 && c>=2) || (a==1 && c==0 && b>=2) || (a>=2 && b==1 && c==0)|| (a>=2 && b==0 && c==1)) flag=4;
        if(flag==4) ans=3+(sum-3)*3;     ///2 1 0
        if((a==0 && b==0 && c>=2) || (b==0 && c==0 && a>=2) || (c==0 && a==0 && b>=2)) flag=5;
        if(flag==5) ans=1+(sum-2)*2;     ///2 0 0
        cout<<ans<<endl;
    }
    return 0;
}


赛后看了下题解,网上有很多用贪心写的

  • 题意:有三种颜色的球各a,b,c种。把他们放到桌子上,放第一颗球的时候不会得到分数,以后的每放一颗球得到的分数等于这颗球左边不同颜色的球个数+右边不同颜色的球个数。
  • 题解:首先尽量把每种颜色的球都放两颗。(假设一共m颗(m<=6))那么一共会得到(1+m-1)(m-1)/2分。并且以后每放下一颗球,最多能够得到m分。
typedef long long ll;
using namespace std;
int main() {
	int a[3];
	while(~scanf("%d%d%d",&a[0],&a[1],&a[2])) {
		ll sum=0,cnt=0;
		for(int i=0;i<2;++i) {
			for(int j=2;j>=0;--j) {
				if(a[j]) {
					--a[j];
					sum+=cnt++;
				}
			}
		}
		printf("%I64d\n",sum+(a[0]+a[1]+a[2])*cnt);
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值