GYM 101606 C.Cued In(贪心)

Description

斯诺克中每个颜色的球进袋分数如下:

img

假设一个人每次都可以打进,上次打进红球则这次必须打彩球,只要桌上还有红球则打进的彩球会被重新放在桌上,给出桌上剩余的 n 个球的颜色,问最多可以得多少分

Input

第一行一整数 n 表示桌上球数,之后输入 n 个球的颜色,保证彩球每种颜色至多一个 ( 1 n 21 )

Output

输出最多得分

Sample Input

5
red
black
pink
red
red

Sample Output

37

Solution

贪心,如果桌上没有彩球则只能打一个红球得一分,否则由于每个红球之后可以拿出一个之前打过的彩球,那么每次打分值最大的彩球显然最优,假设彩球总分为 s u m ,最大分值为 m a x ,红球数量为 n u m ,那么可以通过打一个最大分值的彩球 打一个红球 打一个最大分值的彩球…来得到最高得分,如此方案得到的分数为 m a x ( n u m + 1 ) + n u m + s u m m a x = n u m ( m a x + 1 ) + s u m

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100001;
int deal(char *s)
{
    if(s[0]=='r')return 1;
    if(s[0]=='y')return 2;
    if(s[0]=='g')return 3;
    if(s[0]=='p')return 6;
    if(s[1]=='r')return 4;
    if(s[2]=='u')return 5;
    return 7;
}
int main()
{
    int n,red=0,mx=0,sum=0;
    char s[10];
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",s);
        if(deal(s)==1)red++;
        else
        {
            sum+=deal(s);
            mx=max(mx,deal(s));
        }
    }
    int ans=(red+1)*(mx+1)-1+sum-mx;
    if(mx)printf("%d\n",ans);
    else printf("1\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/v5zsq/article/details/80449845