Tian Ji -- The Horse Racing / HDU - 1052

Posted little-turtle--qjy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tian Ji -- The Horse Racing / HDU - 1052相关的知识,希望对你有一定的参考价值。

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.

Input

The input consists of up to 50 test cases. Each case starts with a positive integer n (n <= 1000) on the first line, which is the number of horses on each side. The next n integers on the second line are the speeds of Tian’s horses. Then the next n integers on the third line are the speeds of the king’s horses. The input ends with a line that has a single 0 after the last test case.

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

题意

就是 传统故事田忌赛马
给出n场比赛
田忌 和 国王 都有n只马
问 田忌 最多可以拿多多少分
(1场比赛 200分 平局 都不得分)

题解

每次循环分2个部分
快马 比较  &&  慢马 比较
快马: 能比国王的最快马 快, 就拿下比赛
         否则 让最慢马 去抵消国王的最快马
慢马: 能比国王的最慢马 快, 就拿下比赛
         否则 去抵消国王的最快马
每次快马或慢马比到比不下去时 就换慢马或快马

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1010;

int n;

int main(){
    while(~scanf("%d", &n)){
        if(n == 0)  break;
        int tian[N], king[N];
        for(int i = 1; i <= n; i++){
            scanf("%d", &tian[i]);
        }
        for(int i = 1; i <= n; i++){
            scanf("%d", &king[i]);
        }
        sort(tian + 1, tian + n + 1);
        sort(king + 1, king + n + 1);
        int tx = 1, ty = n, kx = 1, ky = n, sum = 0;
        bool sign = 1;
        while(true){
            if(sign){//处理最小部分
                if(tian[tx] > king[kx]){//田忌最慢的马比国王的最慢的马快
                    sum++;//直接拿下比赛
                    tx++, kx++;
                    if(tx > ty) break;
                }
                else if(tian[tx] < king[kx]){//田忌最慢的马比国王的最慢的马慢
                    if(tian[tx] < king[ky]) sum--;//必输不可
                    tx++, ky--;//脱国王最快的马下水
                    if(tx > ty) break;
                }
                else{
                    if(tian[ty] == king[ky]){//拖国王的马 入水
                        if(tian[tx] < king[ky]) sum--;
                        tx++, ky--;
                        if(tx > ty) break;
                    }else{
                        sign = false;//比较最快的去啦
                    }
                }
            }else{//处理最大部分
                if(tian[ty] > king[ky]){//田忌最快的马比国王的最快的马快
                    sum++;//直接拿下比赛
                    ty--, ky--;
                    if(tx > ty) break;
                }
                else if(tian[ty] < king[ky]){//田忌最快的马比国王的最快的马慢
                    sum--;//必输不可
                    tx++, ky--;//田忌最慢的马 换 国王最快的马
                    if(tx > ty) break;
                }
                else{
                    if(tian[tx] == king[kx]){
                        if(tian[tx] < king[ky]) sum--;
                        tx++, ky--;
                        if(tx > ty) break;
                    }else{
                        sign = true;//比较最慢的
                    }
                }
            }
        }
        printf("%d
", sum * 200);
    }
    return 0;
}

PS

sum--: 
1. 国王的马是所有马中最快的, 此时不管怎么样, 都会输一局
2. 以慢马 换 国王的快马时 要输
针对 “2”, 为什么 sum-- 前要加 if(tian[tx] < king[ky])
因为有可能出现 田忌最慢马 ?? 国王最快马 速度一样 的情况
那么此时就是平局, 不用 sum--
PS: 不会出现 最慢马??国王最快马快的情况

以上是关于Tian Ji -- The Horse Racing / HDU - 1052的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1052 Tian Ji -- The Horse Racing

POJ 2287 Tian Ji -- The Horse Racing

poj2287 Tian Ji -- The Horse Racing

poj 2287 Tian Ji -- The Horse Racing

ACM--田忌赛马--贪心--HDOJ 1052--Tian Ji -- The Horse Racing

HDU 1052 Tian Ji -- The Horse Racing (贪心)