UPC-5007 High Score(计算)

题目描述
Mårten and Simon enjoy playing the popular board game Seven Wonders, and have just finished a match. It is now time to tally the scores.

One of the ways to score in Seven Wonders is through the use of Science. During the game, the players may collect a number of Science tokens of three different types: Cog, Tablet, and Compass. If a player has a Cogs, b Tablets and c Compasses, that player gets a2 + b2 + c2 + 7 · min(a, b, c) points.

However, the scoring is complicated by the concept of Wildcard Science tokens. For each Wildcard Science token a player has, she may count that as one of the three ordinary types of Science tokens. For instance, the first player in Sample Input 1 has 2 Cogs, 1 Tablet, 2 Compasses, and 1 Wildcard Science, so could thus choose to have the distributions (3, 1, 2), (2, 2, 2) or (2, 1, 3) of Cogs, Tablets and Compasses, respectively. The possible scores for this player are then 32 + 12 + 22 + 7 · 1 = 21, 22 + 22 + 22 + 7 · 2 = 26 and 22 + 12 + 32 + 7 · 1 = 21 depending on how the Wildcard Science is assigned. Thus, the maximum score for this player is 26.
Given the number of tokens each player in the game has, compute the maximum possible score that each of them can achieve if they assign their Wildcard Science tokens optimally.
输入
The input consists of:
• One line with an integer n (3 ≤ n ≤ 7), the number of players in the game.
• n lines, each with four integers a, b, c, d (0 ≤ a, b, c, d ≤ 109 ), giving the number of Cog,Tablet, Compass, and Wildcard Science tokens of a player.
输出
For each player, in the same order they are given in the input, output the maximum score the player may get.
样例输入
3
2 1 2 1
3 2 1 0
1 3 0 1
样例输出
26
21
18

题意:给出a,b,c,d,我们可以对a,b,c中某个数做+1的操作d次,使得最后a^2+b^2+c^2+7*min(a,b,c)最大。
我们取两种策略,一种,把d全部加到a,b,c中的最大值上,因为平方的操作会使其结果大幅增高。
另一种情况,当三者较平均但不绝对平均时,并且d较小时,我们可能先补上最小值的结果会更大。那么我们做的操作就是尽量利用d将三者平齐,也就是填补三者之间的差距,那么对于最小和次小,补齐最小,如果还有d剩余,补齐已经相同的最小和次小,使之和最大平齐,三者差距接近时结果最大。
将上述两种情况的结果都算出来。取最大值即最优解。

#include<bits/stdc++.h>
#define LL long long
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        LL a[4],d;
        scanf("%lld%lld%lld%lld",&a[0],&a[1],&a[2],&d);
        sort(a,a+3);
        LL maxn=a[0]*a[0]+a[1]*a[1]+(a[2]+d)*(a[2]+d)+7*a[0];
        LL minn=a[1]-a[0];
        if(d>minn)
        {
            a[0]+=minn;
            d-=minn;
            minn=a[2]-a[1];
            if(d/2>=minn)
            {
                a[0]+=minn;
                a[1]+=minn;
                d-=2*minn;
                a[0]+=d/3;
                a[1]+=d/3;
                a[2]+=d/3;
                a[2]+=d%3;
            }
            else
            {
                a[0]+=d/2;
                a[1]+=d/2;
                d%=2;
                a[0]+=d;
            }
        }
        else a[0]+=d;
        LL ans=a[0]*a[0]+a[1]*a[1]+a[2]*a[2]+7*min(a[0],min(a[1],a[2]));
//        printf("%lld %lld %lld\n",a[0],a[1],a[2]);
//        printf("------%lld   %lld\n",ans,maxn);
        printf("%lld\n",max(ans,maxn));
    }
}
posted @ 2018-04-16 22:56  KuroNekonano  阅读(120)  评论(0编辑  收藏  举报