hdu1052Tian Ji -- The Horse Racing(贪心算法)

来源:互联网 发布:坐标系转换软件 编辑:程序博客网 时间:2024/06/06 00:24

Tian Ji -- The Horse Racing

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


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
392 83 7195 87 74220 2020 20220 1922 180
 

Sample Output
20000
 


题目大意:

中国古代的历史故事“田忌赛马”是为大家所熟知的。话说齐王和田忌又要赛马了,他们各派出N匹马(N≤2000),每场比赛,输的一方将要给赢的一方200两黄金,如果是平局的话,双方都不必拿出钱。现在每匹马的速度值是固定而且已知的,而齐王出马也不管田忌的出马顺序。请问田忌该如何安排自己的马去对抗齐王的马,才能赢最多的钱?


算法分析:

这个问题很显然可以转化成一个二分图最佳匹配的问题。把田忌的马放左边,把齐王的马放右边。田忌的马A和齐王的B之间,如果田忌的马胜,则连一条权为200的边;如果平局,则连一条权为0的边;如果输,则连一条权为-200的边

然而我们知道,二分图的最佳匹配算法的复杂度很高,无法满足N2000的要求。

我们不妨用贪心思想来分析一下问题。因为田忌掌握有比赛的“主动权”,他总是根据齐王所出的马来分配自己的马,所以这里不妨认为齐王的出马顺序是按马的速度从高到低出的。由这样的假设,我们归纳出如下贪心策略:

Ø 1、如果田忌剩下的马中最强的马都赢不了齐王剩下的最强的马,那么应该用最差的一匹马去输给齐王最强的马。

Ø 2、如果田忌剩下的马中最强的马可以赢齐王剩下的最强的马,那就用这匹马去赢齐王剩下的最强的马。

Ø 3、如果田忌剩下的马中最强的马和齐王剩下的最强的马打平的话,可以选择打平或者用最差的马输掉比赛。

我们发现,第三个贪心策略出现了一个分支:打平或输掉。如果穷举所有的情况,算法的复杂度将比求二分图最佳匹配还要高;如果一概而论的选择让最强的马去打平比赛或者是让最差的马去输掉比赛,则存在反例:

ü 光是打平的话,如果齐王马的速度分别是1 2 3,田忌马的速度也是1 2 3,每次选择打平的话,田忌一分钱也得不到,而如果选择先用速度为1的马输给速度为3的马的话,可以赢得200两黄金。

ü 光是输掉的话,如果齐王马的速度分别是1 3,田忌马的速度分别是2 3,田忌一胜一负,仍然一分钱也拿不到。而如果先用速度为3的马去打平的话,可以赢得200黄金。

虽然因为第三个贪心出现了分支,我们不能直接的按照这种方法来设计出一个完全贪心的方法,但是通过上述的三种贪心策略,我们可以发现,如果齐王的马是按速度排序之后,从高到低被派出的话,田忌一定是将他马按速度排序之后,从两头取马去和齐王的马比赛。有了这个信息之后,动态规划的模型也就出来了!

设f[i,j]表示齐王按从强到弱的顺序出马和田忌进行了i场比赛之后,从“头”取了j匹较强的马,从“尾”取了i-j匹较弱的马,所能够得到的最大盈利。

状态转移方程如下:

其中g[i,j]表示田忌的马和齐王的马分别按照由强到弱的顺序排序之后,田忌的第i匹马和齐王的第j匹马赛跑所能取得的盈利,胜为200,输为-200,平为0

本题小结:

虽然本题存在直接贪心的方法,不过它可以作为一个例子告诉大家,合理的运用贪心策略,分析出问题的一些本质之后,一些看似不能用动态规划做的题目便可以巧妙的确立出状态,继而可以用动态规划来求解。


代码如下:

#include <iostream>
#include <algorithm>
#include<cstdio>
using namespace std;
int n;
int Sp_A[1001];
int Sp_B[1001];
bool cmp(int x,int y)
{
     return x > y;
}
int main()
{
    while(cin>>n)
    {
          if(n == 0)
               break;
          for(int i = 0; i < n; i++)
          {
                  scanf("%d",&Sp_A[i]);
          }
          for(int i = 0; i < n; i++)
          {
                  scanf("%d",&Sp_B[i]);
          }


          sort(Sp_A,Sp_A+n,cmp);
          sort(Sp_B,Sp_B+n,cmp);
          int s1,s2,e1,e2;
          int k = 0;
          s1 = s2 = 0;   //厉害的马
          e1 = e2 = n-1; //差的马
          while(s1<=e1)
          {
                  if(Sp_A[s1] > Sp_B[s2])
                  {
                         k++;
                         s1++;
                         s2++;
                  }
                  else if(Sp_A[s1] < Sp_B[s2])
                  {
                      if(Sp_A[e1]<Sp_B[s2])
                         k--;
                        e1--;
                        s2++;
                  }
                  else  //当前两人的最快的马的速度相等
                  {
                         if(Sp_A[e1]>Sp_B[e2])   //如果田忌的最慢的马能赢国王的最慢的马,则先比较最慢的马。
                            {
                                e1--;
                                e2--;
                                k++;
                            }
                            else     //田忌的最慢的马不能赢国王的最慢的马。则用最慢的马跟国王的最快的马比。
                            {
                                if(Sp_A[e1]<Sp_B[s2])
                                    k--;
                                e1--;
                                s2++;
                            }
                  }
          }
          cout<<200*k<<endl;
    }
    return 0;
}

0 0
原创粉丝点击