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;
}
目录
相关文章
|
4月前
HJ24 合唱队
HJ24 合唱队
23 0
|
4月前
HJ16 购物单
HJ16 购物单
22 0
|
6月前
hdu 1052 Tian Ji -- The Horse Racing【田忌赛马】(贪心)
hdu 1052 Tian Ji -- The Horse Racing【田忌赛马】(贪心)
26 0
|
6月前
DFS56L TF RH1M KK ABB 120 TAS.580.0560G00
DFS56L TF RH1M KK ABB 120 TAS.580.0560G00
30 0
|
7月前
|
机器学习/深度学习 人工智能 监控
UR8FH    A404k    P0926GX FBM233
UR8FH    A404k    P0926GX FBM233
47 0
|
12月前
【CodeForces】夜深人静写cf
Problem - B - Codeforces
42 0
CF711D-Directed Roads(组合数学 dfs找环)
CF711D-Directed Roads(组合数学 dfs找环)
62 0
CF711D-Directed Roads(组合数学 dfs找环)
洛谷P6207-[USACO06OCT] Cows on Skates G(DFS记录路径)
洛谷P6207-[USACO06OCT] Cows on Skates G(DFS记录路径)