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;
    }
    

    东北日出西边雨 道是无情却有情
  • 相关阅读:
    分布式大数据高并发的web开发框架
    用户安全登录问题
    成功扩展live555支持ipv6,同时支持RTSPServer & RTSPClient
    经过两个多月的攻关,终于搞定了live555多线程并稳定压测通过
    经过两个多月的攻关,终于搞定了live555多线程并稳定压测通过
    如何使用EasyNVR+CDN突破萤石云在直播客户端数量上的限制,做到低成本高性价比的直播
    EasyNVR完美搭配腾讯云CDN/阿里云CDN进行RTMP、HLS直播加速的使用说明
    NVR硬件录像机web无插件播放方案功能实现之相关接口注意事项说明
    EasyNVR实现海康、大华NVR硬盘录像机Web无插件播放方案(支持取特定时间段视频流)
    EasyNVR无插件直播服务如何配合EasyBMS使用以及实现流媒体管理功能概述
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/9158363.html
Copyright © 2011-2022 走看看