• 田忌赛马


    搞了我很久啊~~!!~!~

    Tian Ji -- The Horse Racing

    Tian Ji -- The Horse Racing

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 55   Accepted Submission(s) : 15

    Font: Times New Roman | Verdana | Georgia

    Font Size:

    Problem Description

    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
    

    Source

    2004 Asia Regional Shanghai
    贪心策略:
    有多少匹马就比多少次:
    田忌慢马与国王慢马相比:
      田忌慢马比国王慢马快 直接赢;
      田忌慢马比国王快马慢 与 国王快马 拼掉 输一场
     
    田忌慢马  与  国王慢马 相等 分情况:
    (1)田忌快马 比 国王 快马 快    直接赢
    (2)田忌快马 比国王 快马 慢     用田忌慢马 与国王快马拼 输一场
    (3) 田忌快马 与国王 快马相等
       1. 田忌慢马与国王快马 相等      平掉快马;
     2 .   田忌慢马比国王快马慢         用田忌慢马 与国王快马拼 输一场
     
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int tian[1005];
    int king[1005];
    int main()
    {
        int n;
        while(scanf("%d",&n),n)
        {
            int i;
            int qian=0;
            int mintian=0;
            int maxtian=n-1;
            int minking=0;
            int maxking=n-1;
            //初始化数据
            memset(tian,0,sizeof(tian));
            memset(king,0,sizeof(king));
            //输入数据
            for(i=0;i<n;i++)
            {
                scanf("%d",&tian[i]);
            }
            for(i=0;i<n;i++)
            {
                scanf("%d",&king[i]);
            }
            //排序;
            sort(tian,tian+n);
            sort(king,king+n);
            i=n;
            while(i--)
            {
                if(tian[mintian]<king[minking])//田慢马比王慢马 慢 直接与王快马输掉
                {
                    mintian++;
                    maxking--;
                    qian=qian-200;
                    
                }
                 else if(tian[mintian]>king[minking])//田慢马比王慢马 快 直接与王慢马赢掉
                {
                    mintian++;
                    minking++;
                    qian=qian+200;
                    
                }
                 else//田慢马 与王慢马相等 比田快马与王快马
                {
                    if(tian[maxtian]>king[maxking])//田快马 比王快马 快 直接赢一盘 
                    {
                    
                        maxtian--;
                        maxking--;
                        qian=qian+200;//赢两百
    
                        
                    }
                    else //田快马 与 王快马相等 输一盘
                    {
                        if(tian[mintian]<king[maxking])//田慢马 比王 快马慢 直接比掉
                        {
                            mintian++;
                            maxking--;
                            qian=qian-200;
                        }
                        if(tian[mintian]==king[maxtian])//田慢马与王快马相等 快马比掉
                        {
                            maxking--;
                            maxtian--;
                            
                        }
                        
                    
                    }
                
                    
                
            
                
                }
    
    
            
            
            
            }
            printf("%d
    ",qian);
            
        }
    
    
    
    
    
        return 0;
    }
     
  • 相关阅读:
    【Python五篇慢慢弹(3)】函数修行知python
    【Python五篇慢慢弹】数据结构看python
    【项目管理】GitHub使用操作指南
    【Python五篇慢慢弹】快速上手学python
    【NLP】十分钟快览自然语言处理学习总结
    【NLP】条件随机场知识扩展延伸(五)
    【NLP】基于统计学习方法角度谈谈CRF(四)
    【NLP】基于机器学习角度谈谈CRF(三)
    【NLP】基于自然语言处理角度谈谈CRF(二)
    【NLP】前戏:一起走进条件随机场(一)
  • 原文地址:https://www.cnblogs.com/2013lzm/p/3259219.html
Copyright © 2020-2023  润新知