泡泡堂

本文介绍了一种解决类似于田忌赛马问题的方法,并通过C++代码实现了解决方案。该问题关注如何通过策略安排来最大化比赛得分。文章提供了一个算法,通过比较双方实力最接近的选手来决定比赛结果。

点击打开链接

类似田忌赛马,自己看了一天不知道怎么做,不知道怎么贪心,上网一搜都说大水题,原来是先看自己最弱的和对方最弱的能否战胜,不能的花看最强的和对方最强的,能否战胜,不能的话,用自己最弱的消耗掉对方最强的,在一场比赛中总分为2*n

设 甲 胜 x场 平y场 输z场

     乙 输 x场 平y场 赢z场

   x+y+z=n;

2*x+y+y+2*z=2*(x+y+z)=z*n;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int n,aa[100005],bb[100005];
int cal(int a[],int b[])
{
    int sum=0;
    int st1=1,ed1=n,st2=1,ed2=n;
    while(st1<=ed1&&st2<=ed2)
    {//cout<<st1<<" "<<ed1<<" "<<st2<<" "<<ed2<<" "<<sum<<endl;
        if(a[st1]>b[st2])
        {
            sum+=2;
            st1++;
            st2++;
        }
        else
        {
            if(a[ed1]>b[ed2])
            {
                sum+=2;
                ed1--;
                ed2--;
            }
            else
            {
                if(a[st1]==b[ed2])
                    sum+=1;
                st1++;
                ed2--;
            }
        }
    }
    return sum;
}
int main()
{
    int i;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        scanf("%d",&aa[i]);
    for(i=1;i<=n;i++)
        scanf("%d",&bb[i]);
    sort(aa+1,aa+1+n);
    sort(bb+1,bb+1+n);
    cout<<cal(aa,bb)<<" ";
    cout<<2*n-cal(bb,aa)<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值