HDU杭电1052 Tian Ji -- The Horse Racing解题报告

本人第一次写博客,希望各位大神多多指导与包涵,不足的地方还请指出,新手在此谢过啦!!!

题目描述:

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26442    Accepted Submission(s): 7799

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

 本题大意就是先输入比赛马匹的个数如果为0,结束程序,然后依次输入田忌与齐桓王的马匹速度,每赢一局输的须向赢得支付200元,最后输出总共田忌赢得钱数或输的钱数。。。

附上个人认为比较***钻的数据:

10

12 3 4 5 23 5 23 6 12 8

23 6 7 8 8 9 23 4 6 3

 

附上AC代码:

//主题是运用C语言,但是运用了c++的排序手法

#include<iostream>

#include<algorithm>

using namespace std;

int main()

{

    int a[1050];

    int b[1050];

    int m;

    while(scanf("%d",&m)!=EOF)

    {

        if(m==0)

            break;

        for(int i=0;i<m;i++)

        {

            scanf("%d",&a[i]);

        }

        for(int i=0;i<m; i++)

        {

            scanf("%d",&b[i]);

        }

        sort(a,a+m);

        sort(b,b+m);

       reverse(a,a+m);

       reverse(b,b+m);

        int tk=0,tm=m-1,wk=0,wm=m-1,win=0;

//定义变量;

       while(m--)

        {

                if(a[tk]>b[wk])

                {

                    win++;

                    wk++;

                    tk++;

                }

                else if(a[tm]>b[wm])

                {

                    win++;

                    tm--;

                    wm--;

                }

                else if(a[tm]<b[wk])

                {

                    win--;

                    tm--;

                    wk++;

                }

//可能存在平局的现象所以应在此加上第三个条件

        }

       printf("%d\n",win*200);

    }

}

 

错误思路:先将田忌和齐桓王的马分别进行降序,再拿田忌的最厉害的马跟齐桓王的马比,对齐桓王的马进行循环,当出现田忌的马可以战胜齐桓王的马时便win++,接着将比赛过后的马进行赋值为0break跳出齐桓王循环,对田忌进行下一次循环,若该匹马不等于0时,在进行判断。同时还得判断平局的次数,最后总马匹量-胜利局数减去平局即为输的次数,最后win*200;结束

实际上,这样做并不能得到真正的结果,

10

12 3 4 5 23 5 23 6 12 8

23 6 7 8 8 9 23 4 6 3

以上实验思路所写代码实现上组数据时得到的结果是400,而正确答案是1000

因为在此田忌的最差的马并没有实现自己的最大价值。

正确解题思路如下:

先分别对田忌与齐桓王的马匹进行降序排,先拿田忌最厉害的马跟齐桓王的马进行比,假使田忌可以战胜,则win++,假使不能战胜齐桓王的马匹,则用田忌最差的马拿出来进行比较,跟齐桓王的最差的马进行比较(能赢则赢,尽量减少平局的次数,为什么要和齐桓王的最差的马进行比较,想一下就知道为什么啦),如果田忌最差的马不能战胜齐桓王最差的马(开始献身的价值到了)用田忌最差的马跟齐桓王最厉害的马进行比较【因此在此有三个判断,每次只执行一次判断,每次判断后应进行相应数据的递减或递增】

 

此句话不能直接写成else{..........

....

}

因为到最后一次如果出现平局的话win是不能减一的;

到最后循环结束,输出win*200

该题主要还是得看想法,理解田忌赛马怎样用编程实现,实现每匹马的最大值!!!!!!!

posted on 2016-07-31 11:46  ZHOUXIAOHAO  阅读(414)  评论(0编辑  收藏  举报

导航