hdu4811-Ball(2013ACM/ICPC亞洲區南京站現場賽)

題目連接:http://acm.hdu.edu.cn/showproblem.php?pid=4811php

題目描述:ios

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

題意:spa

有三種顏色,給你每種顏色的球的數量,有如下兩種得分方式,問你如何放置這些球,讓總得分最大。code

方式一:放第一個球的得分爲0blog

方式二:放在最後面的得分爲以前的全部球的顏色種數three

方式三:放在中間的得分爲左邊球的顏色種數+右邊球的顏色種數ip

思路:ci

找規律,推導出公式,由於只有三種顏色,若是每種顏色都有2的及以上,那麼能夠先在兩邊各擺三種顏色的球,這樣每次把其餘球放入中間時都能獲得6分,即ans=(R-2+Y-2+B-2)*6+15(15爲在兩邊各擺三種顏色的球的過程所得到的總得分)。get

其實若是一個球的數量超過了2,那麼剩下的就是一個乘法了。 這個理解很簡單,由於超過了2的話,說明最優的方案必定是左右各一個,否則若是都在一邊的話就只得1分了。input

代碼:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> typedef long long ll; using namespace std; ll a[3][3][3];//a[i][j][k]表示紅色球爲i個,黃色球爲j個,藍色球爲k個的排列總數
ll f[4],n,k,tep; int main() { a[0][0][0]=0,a[0][0][1]=0,a[0][0][2]=1,a[0][1][1]=1,a[0][1][2]=3,a[0][2][2]=6; a[1][1][1]=3,a[1][1][2]=6,a[1][2][2]=10,a[2][2][2]=15; while (cin>>f[1]>>f[2]>>f[3]) { n=k=0; for (int i=1; i<4; i++) { if (f[i]>2) k=2; else k=f[i]; n+=f[i]-k,f[i]=k; } sort(f+1,f+4); tep=f[1]+f[2]+f[3]; cout<<a[f[1]][f[2]][f[3]]+n*tep<<endl; } return 0; }
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息