zoukankan      html  css  js  c++  java
  • 田忌赛马

    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

    题意 : 就是正常的田忌赛马,赢的话会得到 200 奖金。
    思路分析:将马的速度排一下序,田忌要想赢得更多的奖金,那么当他的最好的马比不上齐王最好的马时,就要用自己最差的马去和齐王去比,如果这么一想的话就是一个 dp,
      dp[i][j]表示到第 i 场比赛时,使用 j 匹差马的最大收益。
      dp[i][j] = max(dp[i-1][j]+score(a[i-j], b[i]), dp[i-1][j-1]+score(a[n-j+1], b[i]));
    代码示例:
    int n;
    int a[1005], b[1005];
    bool cmp(int x, int y){
        return x > y;
    }
    int dp[1005][1005];
    
    int score(int x, int y){
        if (x > y) return 1;
        if (x == y) return 0;
        return -1;
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
    
        while(~scanf("%d", &n) && n){
            for(int i = 1; i <= n; i++){
                scanf("%d", &a[i]);        
            }
            for(int i = 1; i <= n; i++){
                scanf("%d", &b[i]);        
            }
            sort(a+1, a+1+n, cmp);
            sort(b+1, b+1+n, cmp);
            
            memset(dp, 0x8f, sizeof(dp));
            dp[0][0] = 0;
            
            for(int i = 1; i <= n; i++){
                for(int j = 0; j <= i; j++){
                    if (j == 0){
                        dp[i][0] = dp[i-1][0]+score(a[i], b[i]);
                    }
                    else {
                        dp[i][j] = max(dp[i-1][j]+score(a[i-j], b[i]), dp[i-1][j-1]+score(a[n-j+1], b[i]));
                    }
                }
            }        
            int ans = -inf;
            for(int i = 0; i <= n; i++) ans = max(ans, dp[n][i]);
            printf("%d
    ", ans*200);
        }
        return 0;
    }
    

    东北日出西边雨 道是无情却有情
  • 相关阅读:
    跨域通信
    Http协议-报文
    从快递公司作业模式看网络通信
    xx.exe 中的 0x7c92e4df 处最可能的异常: 0xC0000008: An invalid handle was specified
    [wp8游戏] cocos2d-x v2.2 + VS2013 环境搭建
    wp加载本地HTML(附带图片,CSS,JS)
    谈谈.NET程序集(一)
    [读书心得] .NET中 类型,对象,线程栈,托管堆在运行时的关系
    矩估计与最大似然估计
    WP8开发札记(二)让程序支持锁屏运行
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/9158363.html
Copyright © 2011-2022 走看看