Tian Ji -- The Horse Racing 田忌赛马

简介: Tian Ji -- The Horse Racing 田忌赛马

1、题目描述

Here is a famous story in Chinese history.\
\
"That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others."\
\
"Both of Tian and the king have three horses in different classes, namely, regular, plus, and super. The rule is to have three rounds in a match; each of the horses must be used in one round. The winner of a single round takes two hundred silver dollars from the loser."\
\
"Being the most powerful man in the country, the king has so nice horses that in each class his horse is better than Tian's. As a result, each time the king takes six hundred silver dollars from Tian."\
\
"Tian Ji was not happy about that, until he met Sun Bin, one of the most famous generals in Chinese history. Using a little trick due to Sun, Tian Ji brought home two hundred silver dollars and such a grace in the next match."\
\
"It was a rather simple trick. Using his regular class horse race against the super class from the king, they will certainly lose that round. But then his plus beat the king's regular, and his super beat the king's plus. What a simple trick. And how do you think of Tian Ji, the high ranked official in China?"\
\
\
\
Were Tian Ji lives in nowadays, he will certainly laugh at himself. Even more, were he sitting in the ACM contest right now, he may discover that the horse racing problem can be simply viewed as finding the maximum matching in a bipartite graph. Draw Tian's horses on one side, and the king's horses on the other. Whenever one of Tian's horses can beat one from the king, we draw an edge between them, meaning we wish to establish this pair. Then, the problem of winning as many rounds as possible is just to find the maximum matching in this graph. If there are ties, the problem becomes more complicated, he needs to assign weights 0, 1, or -1 to all the possible edges, and find a maximum weighted perfect matching...\
\
However, the horse racing problem is a very special case of bipartite matching. The graph is decided by the speed of the horses --- a vertex of higher speed always beat a vertex of lower speed. In this case, the weighted bipartite matching algorithm is a too advanced tool to deal with the problem.\
\
In this problem, you are asked to write a program to solve this special case of matching problem.
Output

For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.\
Sample Input

3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
0

Sample Output

200
0
0

Source

2004 Asia Regional Shanghai

2、思路分析

以上题目为杭电oj的1025题,题目的背景正是大家耳熟能详的田忌赛马,题目的意思便是要让己方的马能够赢得最多的场数。

我们可以这样做,竟可能的用强的马去跑赢强的马,跑不赢的就用己方劣势的马去消耗对面强的马,具体思路如下:

首先我们应该要先比较两方最快的马,这时候会有两种情况:
(一)本方大于对方
直接用本方最快的马去干掉对方最快的马。

(二)本方小于等于对方
    本方最快的马跑不过对面最快的马的话,我们只能选择己方劣势的马来消耗掉对面的强马。
    所以我们应该要比较双方最慢的马,这时候也会有两种情况:
    1、本方大于对方
    这时候我们可以直接用本方最慢的马去干掉对方最慢的马

    2、本方小于等于对方
        本方最慢的马与对方最快的马作比较(只可能是小于或等于)
        (1)本方等于对方
        打平
        (2)本方小于对方
        换子,消耗掉对面的最强马。

重复以上步骤,直到全部比完,我们也就可以得出答案。

3、AC代码

#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF&&n)  //马的数量
    {
        int a[1001],b[1001],i,sum=0;
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);     //本方
        for(i=0;i<n;i++)
            scanf("%d",&b[i]);     //对方
        sort(a,a+n);               //排序
        sort(b,b+n);
        int bl=0,br=n-1,al=0,ar=n-1; //四个端点
        //printf("%d\n",a[ar]);
        while(al<=ar)                //终止条件
        {
            if(a[ar]>b[br])          //最快斗最快
            {
                sum+=200;
                ar--;
                br--;
            }
            else      
            {
                if(a[al]>b[bl])      //最慢斗最慢
                {
                    sum+=200;
                    al++;
                    bl++;
                }
                else
                {
                    if(a[al]==b[br])  //打平
                    {
                        al++;
                        br--;
                    }
                    else              //换子
                    {
                        sum-=200;
                        al++;
                        br--;
                    }
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}
目录
相关文章
|
8月前
|
人工智能 数据可视化 C++
OpenClaw 与 Hermes 全面对比与一键部署指南
2026年AI智能体爆发,OpenClaw(24小时在线秘书,适配钉钉/微信等,快速上手)与Hermes(自进化型助理,擅复杂任务与自主学习)成两大热门开源框架。本文深度对比+阿里云一键部署指南,助你零门槛启用AI Agent!
874 14
|
8月前
|
安全 BI
HIS医疗信息化核心:护士工作站9大模块详解
HIS系统是医疗数字化核心,涵盖EMR、收费管理、医护工作站等模块。其中护士工作站分住院与门诊两部分:住院站实现医嘱闭环、护理执行、床位管理、计费协同及统计分析;门诊站支持分诊叫号、医嘱执行、检查预约等,全面提升护理效率与患者安全。
672 1
|
监控 算法 API
速卖通商品详情API文档
速卖通商品列表API(aliexpress.item_search)支持关键词搜索、类目筛选与多维度排序,可批量获取商品标题、价格、销量等信息,适用于比价、选品及库存监控。采用AppKey+Token认证,保障数据安全,助力电商自动化运营。
|
存储 数据挖掘 数据处理
掌握Pandas核心数据结构:Series与DataFrame的四种创建方式
本文介绍了 Pandas 库中核心数据结构 Series 和 DataFrame 的四种创建方法,包括从列表、字典、标量和 NumPy 数组创建 Series,以及从字典、列表的列表、NumPy 数组和 Series 字典创建 DataFrame,通过示例详细说明了每种创建方式的具体应用。
1532 67
|
域名解析 小程序 Linux
CentOS搭建内网穿透frp并绑定自定义域名
CentOS搭建内网穿透frp并绑定自定义域名
4171 2
|
存储 人工智能 监控
通过Milvus和Langchain快速构建基于百炼大模型的LLM问答系统
阿里云向量检索服务Milvus版是一款全托管向量检索引擎,并确保与开源Milvus的完全兼容性,支持无缝迁移。它在开源版本的基础上增强了可扩展性,能提供大规模AI向量数据的相似性检索服务。凭借其开箱即用的特性、灵活的扩展能力和全链路监控告警,Milvus云服务成为多样化AI应用场景的理想选择,包括多模态搜索、检索增强生成(RAG)、搜索推荐、内容风险识别等。您还可以利用开源的Attu工具进行可视化操作,进一步促进应用的快速开发和部署。
1887 4
|
算法 UED 开发者
🚀短链(Short Link):发展动态与工具推荐🚀
短链是将冗长的URL通过特定算法压缩成简短的字符串,便于用户在社交媒体和营销活动中分享。短链不仅美化了链接,还提升了用户体验,并具有易于传播和跟踪分析的优势。本文介绍了短链的作用、发展历程,以及在goo.gl退出后,推荐的替代工具如Dub.co,帮助用户继续享受短链带来的便利与营销价值。
2121 1
🚀短链(Short Link):发展动态与工具推荐🚀
|
自然语言处理 开发者
多模态大模型LLM、MLLM性能评估方法
针对多模态大模型(LLM)和多语言大模型(MLLM)的性能评估,本文介绍了多种关键方法和标准,包括模态融合率(MIR)、多模态大语言模型综合评估基准(MME)、CheckList评估方法、多模态增益(MG)和多模态泄露(ML),以及LLaVA Bench。这些方法为评估模型的多模态和多语言能力提供了全面的框架,有助于研究者和开发者优化和改进模型。
2416 5