HUD1052 __ P2 1002 __ Tian Ji -- The Horse Racing

田忌赛马

原问题

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.

提炼

田忌赛马问题的一般解, 第一行输入马数n, 第二行n个数据为田忌的马的速度, 第三行为国王的马的速度
赢一场+200, 输一场-200
输出最后田忌可以获得的钱数

解(C)
#include <stdio.h>
#include <string.h>
int tianH[1001];
int kingH[1001];
void quickSort(int a[], int begin, int end)
{
    if(begin < end)
    {
        int i = begin, j = end;
        int vot = a[i];
        while(i != j)
        {
            while(i < j && vot >= a[j])
                j--;
            if(i < j)
                a[i++] = a[j];
            while(i < j && a[i] >= vot)
                i++;
            if(i < j)
                a[j--] = a[i];
        }
        a[j] = vot;
        quickSort(a, begin, j - 1);
        quickSort(a, i + 1, end);
    }
}
int race(int tian[], int king[], int n)
{
    quickSort(tian, 0, n - 1);
    quickSort(king, 0, n - 1);
    int h1,h2,t1,t2;
    int w=0,l=0;
    h1 = 0, h2 = 0, t1 = n - 1, t2 = n - 1;

    for(int i = 0; i < n; i++)
    {
        if(tian[h1] > king[h2])
        {
            w++;
            h1++;
            h2++;
        }
        else
        {
            if(tian[t1] > king[t2])
            {
                w++;
                t1--;
                t2--;
            }
            else
            {
                if(!(tian[t1] == king[h2]))
                {
                    l++;
                    t1--;
                    h2++;
                }
            }
        }
    }
    return (w-l) * 200;
}

main()
{
    int n;
    while(scanf("%d", &n))
    {
        if(!n)
            break;
        int ans = 0;

        for(int i = 0; i < n; i++)
            scanf("%d", &tianH[i]);
        for(int i = 0; i < n; i++)
            scanf("%d", &kingH[i]);

        printf("%d\n", race(tianH, kingH, n));
    }
}
分析

这道题算是比较水的, 主要是贪心和具体的策略一般化.
具体是三个策略:

  1. 田忌最快的马可以赢齐王最快的马时, 直接赢
  2. 田忌最慢的马可以赢齐王最慢的马时, 直接赢
  3. 当田忌的最慢的马不能赢时, 用这匹马和齐王最快的马比
    (这里要注意的是最快最慢是相对与当前还没有比赛的马来说的, 见下)

这里比较重要的一点是利用角标一点一点缩小问题的规模, 只要想到每次都只需考虑当前最慢最快的四匹马之间如何比较即可.

总结

题目本身比较水, 贪心算法本身写起来也没什么难度, 主要是要静下心来思考问题的一般解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值