18行代码AC_HDU-4811 Ball(数学、推公式)

Problem Description

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 109.

Output

For each test case, print the answer in one line.


题目大意:

有若干个红球、黄球、蓝球,依次放球,在桌子上排成一排

1、第一个球记0分。

2、如果该球放在这一排的两端,得分为除该球的颜色外桌子上其他颜色的球的种类数。 如:桌子上有100个黄球,100个红球,第201次放了一个蓝球在这一排球的两端,则得分+2

3、如果该球放在两个球中间,得分为该球左边不同颜色的球的种类数+该球右边不同颜色的球的种类数。如:球的排列为:红蓝 蓝红 ,在两个蓝球中间放任意球,得分都+4; 再如球的排列为红红蓝黄 红蓝黄,在第四个-第五个球之间放任意球,得分都+6

因此最大得分的排列方法是:先在每一列的左右两端各放一个不同颜色的球,之后每一个球都放在中间,当左右两端的球的颜色种类为3时, 无论放什么颜色的球,加分都是6。

注意:这句话误导性很大:
he scores queals to the number of different colors of the balls before…
翻译: 得分等于不同颜色的球的种类的数量,而不是不同颜色的球的数量


读完题得到的结论是: 没有算法知识,是一道推导数学公式的题。

当输入2 2 2时, 输出0+1+2+3+4+5=15

当输入3 3 3时, 输出0+1+2+3+4+5+6+6+6=33

当输入4 4 4时,输出0+1+2+3+4+5+6+6+6+6+6+6=51

当输入2 1 5时,输出0+1+2+3+4+5+5+5
2+1+2=5,因此为0+1+2+3+4。4的后一个数是5。 5-2=3,因此为5+5+5

当输入1 1 5时,输出0+1+2+3+4+4+4
1+1+2=4,因此为0+1+2+3。3的后一个数是4。5-2=3,因此为4+4+4

很容易推导出结论,当红、黄、蓝球的个数在2个以下时,按0+1+…递增,当遍历到其两个以上的个数时,按递增后+1的值累加。 读不明白没关系, 看看代码就理解了

最开始用循环处理递增加法,结果提交超时,改为高斯公式,AC


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    
    
	ios::sync_with_stdio(false);
	ll a, b, c; while(cin>>a>>b>>c) {
    
    
		ll sum1 = 0, sum2=0;
		if(a<=2) sum1+=a;
		else {
    
     sum1+=2; sum2+=(a-2); }
 		
		if(b<=2) sum1+=b;
		else {
    
     sum1+=2; sum2+=(b-2); }
		
		if(c<=2) sum1+=c;
		else {
    
     sum1+=2; sum2+=(c-2); }
		
		ll sum = 0;

//		for(int i = 0; i < sum1; i++) 	sum += i;
		sum += ((sum1-1)*sum1)/2;

//		for(int i = 0; i < sum2; i++)   sum += sum1;
		sum = sum+sum2*sum1;

		cout << sum << endl;
		
	}
return 0; }

猜你喜欢

转载自blog.csdn.net/weixin_43899069/article/details/108276947
今日推荐