贪心算法 田忌赛马(problem C)

题意:这是田忌赛马的故事,首先可以将田忌和国王的马的速度从快到慢排列,然后进行比较。每比一次两边各减去一匹马,用四个标记分别标记田忌和国王的最快、最慢马。先比较他们最快的马,若相等,再比较最慢的马,考虑一下条件和其他因素就可以了。

#include"stdio.h"

#include"algorithm"
using namespace std;
const int N=1010;
int tian[N];
int king[N];
int cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int n,i;
    while(scanf("%d",&n)&&n)
    {
        for(i=0;i<n;i++)
            scanf("%d",&tian[i]);
        for(i=0;i<n;i++)
            scanf("%d",&king[i]);
        sort(tian,tian+n,cmp);
        sort(king,king+n,cmp);
        int tfirst=0,kfirst=0,tlast=n-1,klast=n-1;
        int counta=0;
        for(i=0;i<n;i++)
        {
            if(tian[tfirst]>king[kfirst])
            {
                tfirst++;
                kfirst++;
                counta++;
            }
            else if(tian[tfirst]<king[kfirst])
            {
                tlast--;
                kfirst++;
                counta--;
            }
            else
            {
                if(tian[tlast]>king[klast])
                {
                    tlast--;
                    klast--;
                    counta++;
                }
                else if(tian[tlast]<king[klast])
                {
                    tlast--;
                    kfirst++;
                    counta--;
                }
                else
                {
                    if(tian[tlast]<king[kfirst])
                    {
                        tlast--;
                        kfirst++;
                        counta--;
                    }
                }
            }
        }
        printf("%d\n",counta*200);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春哥111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值