F - Tian Ji -- The Horse Racing

题目如下:

Here is a famous story in Chinese history. 

"That was about 2300 years ago. General Tian Ji was a high official in thecountry 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 ofthe horses must be used in one round. The winner of a single round takes twohundred silver dollars from the loser." 

"Being the most powerful man in the country, the king has so nice horsesthat in each class his horse is better than Tian's. As a result, each time theking takes six hundred silver dollars from Tian." 

"Tian Ji was not happy about that, until he met Sun Bin, one of the mostfamous generals in Chinese history. Using a little trick due to Sun, Tian Jibrought home two hundred silver dollars and such a grace in the nextmatch." 

"It was a rather simple trick. Using his regular class horse race againstthe super class from the king, they will certainly lose that round. But thenhis plus beat the king's regular, and his super beat the king's plus. What asimple trick. And how do you think of Tian Ji, the high ranked official inChina?" 

 

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 horseracing problem can be simply viewed as finding the maximum matching in abipartite graph. Draw Tian's horses on one side, and the king's horses on theother. Whenever one of Tian's horses can beat one from the king, we draw anedge between them, meaning we wish to establish this pair. Then, the problem ofwinning as many rounds as possible is just to find the maximum matching in thisgraph. If there are ties, the problem becomes more complicated, he needs toassign weights 0, 1, or -1 to all the possible edges, and find a maximumweighted 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 speedalways beat a vertex of lower speed. In this case, the weighted bipartitematching 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 ofmatching problem. 

---------------------------------------------------------------------------------------------------------------

输入如下:

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

---------------------------------------------------------------------------------------------------------------

输出如下:

For each input case, output a line containing a singlenumber, which is the maximum money Tian Ji will get, in silver dollars. 

---------------------------------------------------------------------------------------------------------------

测试输入如下:

3

92 83 71

95 87 74

2

20 20

20 20

2

20 19

22 18

0

---------------------------------------------------------------------------------------------------------------

测试输出如下

200

0

0

 ---------------------------------------------------------------------------------------------------------------

思路就是我的那篇”田忌赛马“,有兴趣的可以看下,找一下就好了。如果只要代码的话,那就算了吧!

---------------------------------------------------------------------------------------------------------------

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int main()
{
    int a[1000];
    int n;
    int b[1000];    
    int i,s,j,e,sum;
    while(scanf("%d",&n)!=EOF)
    {
    
        if(n==0) break;
        for( i=0;i<n;i++)
          scanf("%d",&a[i]);
        for( i=0;i<n;i++)
          scanf("%d",&b[i]);
        sort(a,a+n);
        sort(b,b+n);
        i=s=0;j=e=n-1;sum=0;
        while(i<=j)
        {
           if(a[i]>b[s]) {sum++;s++;i++;}
            else if(a[i]<b[s]){sum--;e--;i++;}
           else
            {
            if(a[j]>b[e]) {sum++;e--;j--;}
             else{if(a[i]<b[e])sum--;e--;i++;}
            }
        }
        printf("%d\n",sum*200);
    }
    return 0;
} 
---------------------------------------------------------------------------------------------------------------

posted @ 2017-07-10 11:09  让你一生残梦  阅读(127)  评论(0编辑  收藏  举报