Codeforces Round #356 (Div. 2)A. Bear and Five Cards(简单模拟)

本文解析了CodeForces平台上的一道A级题目——小熊Limak的游戏策略。该题要求玩家通过丢弃特定数量的卡片来最小化剩余卡片上的数值总和。文章详细讨论了各种情况下的最优解,并提供了完整的代码实现。
A. Bear and Five Cards
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A little bear Limak plays a game. He has five cards. There is one number written on each card. Each number is a positive integer.

Limak can discard (throw out) some cards. His goal is to minimize the sum of numbers written on remaining (not discarded) cards.

He is allowed to at most once discard two or three cards with the same number. Of course, he won't discard cards if it's impossible to choose two or three cards with the same number.

Given five numbers written on cards, cay you find the minimum sum of numbers on remaining cards?

Input

The only line of the input contains five integers t1, t2, t3, t4 and t5 (1 ≤ ti ≤ 100) — numbers written on cards.

Output

Print the minimum possible sum of numbers written on remaining cards.

Examples
input
7 3 7 3 20
output
26
input
7 9 3 1 8
output
28
input
10 10 10 10 10
output
20

链接:http://codeforces.com/contest/680/problem/A

这是我第一次打cf,做出了div2的A和B,感觉还不错,虽然都很水= =这题看懂了题意之后就是简单的模拟了,我写的代码重复率比较高,大多是复制粘贴的,现在也懒得改了,也许这样能看的更清楚呢!!
只不过这么长的代码,我也是佩服我自己当时能写出来,可能是对于我来说,半夜写代码更有效率吧,,,呵呵。
#include<bits/stdc++.h>
using namespace std;

#define Rep(i,a,b) for(int i=(a);i<=(b);i++)

int mmin(int a,int b,int c)
{
    return min(a,min(b,c));
}

int main()
{
    int a[6]= {0};
    set<int> sei;
    for (int i=1; i<=5; i++)
    {
        cin>>a[i];
        sei.insert(a[i]);
    }
    int sum=0;
    set<int>::iterator its;
    if (sei.size()==5)
    {
        Rep(i,1,5)
        sum+=a[i];
        printf("%d\n",sum);
        return 0;
    }
    else
    {
        if (sei.size()==1)
        {
            sum=2*a[1];
            printf("%d\n",sum);
            return 0;
        }
        else if (sei.size()==2)
        {
            int a1=0,a2=0;
            int te[20]={0};
            int id;
            for (its=sei.begin(),id=0;its!=sei.end();id++,its++)
            {
                te[id]=*its;
            }
            Rep(i,1,5)
            {
                if (a[i]==te[0])
                    a1++;
                else if (a[i]==te[1])
                    a2++;
            }
            if (a1==1||a1==4)
            {
                printf("%d\n",te[0]+te[1]);
                return 0;
            }
            else if (a1==2)
            {
                printf("%d\n",min(te[1]*3,2*te[0]));
                return 0;
            }
            else if (a1==3)
            {
                printf("%d\n",min(te[0]*3,te[1]*2));
                return 0;
            }
        }
        else if (sei.size()==3)
        {
            int a1=0,a2=0,a3=0;
            int te[20]={0};
            int id;
            for (its=sei.begin(),id=0;its!=sei.end();id++,its++)
            {
                te[id]=*its;
            }
            Rep(i,1,5)
            {

                if (a[i]==te[0])
                    a1++;
                else if (a[i]==te[1])
                    a2++;
                else if (a[i]==te[2])
                    a3++;
            }
            int f1=te[0];
            int f2=te[1];
            int f3=te[2];
            if (a1==1&&a2==1)
            {
                printf("%d\n",f1+f2);
                return 0;
            }
            else if (a1==1&&a2==2)
            {
                printf("%d\n",min(f1+2*f3,f1+2*f2));
                return 0;
            }
            else if (a1==1&&a2==3)
            {
                printf("%d\n",f1+f3);
                return 0;
            }
            else if (a1==2&&a2==1)
            {
                printf("%d\n",min(f2+2*f3,f2+2*f1));
                return 0;
            }
            else if (a1==2&&a2==2)
            {
                printf("%d\n",min(2*f1+f3,2*f2+f3));
                return 0;
            }
            else if (a1==3&&a2==1)
            {
                printf("%d\n",f2+f3);
                return 0;
            }
        }
        else if (sei.size()==4)
        {
            int a1=0,a2=0,a3=0,a4=0;
            int te[20]={0};
            int id;
            for (its=sei.begin(),id=0;its!=sei.end();id++,its++)
            {
                te[id]=*its;
            }
            Rep(i,1,5)
            {
                if (a[i]==te[0])
                    a1++;
                else if (a[i]==te[1])
                    a2++;
                else if (a[i]==te[2])
                    a3++;
                else if (a[i]==te[3])
                    a4++;
            }
            int f1=te[0];
            int f2=te[1];
            int f3=te[2];
            int f4=te[3];
            if (a4==2)
            {
                printf("%d\n",f1+f2+f3);
                return 0;
            }
            if (a3==2)
            {
                printf("%d\n",f1+f2+f4);
                return 0;
            }
            if (a2==2)
            {
                printf("%d\n",f1+f3+f4);
                return 0;
            }
            if (a1==2)
            {
                printf("%d\n",f2+f3+f4);
                return 0;
            }
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/s1124yy/p/5575110.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值