田忌赛马(Tian Ji -- The Horse Racing)中的动态规划以及贪心算法

这两天碰到一道看似很简单,但是实际做起来确实比较难的问题,在这里分析讨论一下。

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1052

Tian Ji -- The Horse Racing

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

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?"

田忌赛马(Tian Ji -- The Horse Racing)中的动态规划以及贪心算法_第1张图片

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

Recommend
JGShining

动态规划

因为贪心算法很难想,所以从先从dp入手了,首先对田忌还有王的马都按照从快到慢的顺序排列。

定义f[i][j]为,经过了i场比赛后,田忌从最慢的马开始从后往前使用了j匹马的最大收益

一.

f[i-1][j]表示经过了i-1场比赛后,田忌已经使用了j匹慢马的最大收益。当第i场到来时,为了得到f[i][j],田忌只能使用前面的第i-j匹快马,

此时f[i][j]=f[i-1][j]+(田忌使用第i-j匹马与王的第j匹马拼后的收益,使用函数score(i-j,j)表示)

二.

f[i-1][j-1]表示经过了i-1场比赛后,田忌使用了j-1匹慢马的最大收益。当第i场到来时,为了得到f[i][j],田忌使用了第j匹慢马与王的第i匹快马拼,此时f[i][j]=f[i-1][j-1]+(田忌使用第n-j+1匹马与王的第j匹马拼的最大收益score(n-j+1,j),其中n表示所有总共的马数)

所以:

f[i][j]=max(f[i-1][j]+score(i-j,j),f[i-1][j-1]+score(n-j+1,j))


具体程序实现如下:

[cpp]  view plain copy
  1. <span style="font-size:12px;">#include<stdio.h>  
  2. #include<algorithm>  
  3. using namespace std;  
  4.   
  5. int a[1001],b[1001],f[1001][1001];  
  6. /* 
  7.  *f[i][j]定义为打了i场比赛,从tanji尾部取出j匹马取得的最大收益 
  8.  */  
  9. int score(int i, int j){  
  10.     if(a[i]>b[j])  
  11.       return 1;  
  12.     else if(a[i]<b[j])  
  13.       return -1;  
  14.     else return 0;  
  15. }  
  16. //初始化  
  17. void init(int n){  
  18.     //将f全部设置为0  
  19.     for(int p=0; p<=n; p++)  
  20.       for(int q=0; q<=n; q++)  
  21.         f[p][q]=0;  
  22.     //设置f[i][0],因为无论tianji还是王都是从最快的马按照递减顺序出场  
  23.     //所以每次只需比较当前的两匹马的大小,并在前一次比较的基础上设置当前f值  
  24.     for(int i=1; i<=n; i++){  
  25.         if(a[i]>b[i])  
  26.             f[i][0]=f[i-1][0]+1;  
  27.         else if(a[i]<b[i])  
  28.           f[i][0]=f[i-1][0]-1;  
  29.         else  
  30.           f[i][0]=f[i-1][0];//这里容易漏掉,相等就使用前一次的最优值  
  31.     }  
  32.     //设置f[i][i],因为每一次都是tianji都是用最慢的马和王最快的马拼,所以tianji从后往前  
  33.     //王从前往后比较  
  34.     for(int j=n,g=1; j>=1; j--,g++){  
  35.         if(a[j]>b[g])  
  36.           f[g][g]=f[g-1][g-1]+1;  
  37.         else if(a[j]<b[g])  
  38.           f[g][g]=f[g-1][g-1]-1;  
  39.         else  
  40.           f[g][g]=f[g-1][g-1];  
  41.     }  
  42. }  
  43.   
  44. int maxnum(int i,int j){  
  45.     if(i>j)  
  46.       return i;  
  47.     else   
  48.       return j;  
  49. }  
  50.   
  51. bool compare(const int& a, const int& b){  
  52.     return a>b;  
  53. }  
  54.   
  55. int main(){  
  56.     int n,max;  
  57.     while(1){  
  58.         scanf("%d",&n);  
  59.         if(n==0)  
  60.           break;  
  61.         for(int i=1; i<=n; i++){  
  62.               scanf("%d",&a[i]);  
  63.         }  
  64.         for(int x=1; x<=n; x++){  
  65.               scanf("%d",&b[x]);  
  66.         }  
  67.         sort(a+1,a+n+1,compare);  
  68.         sort(b+1,b+n+1,compare);  
  69.         init(n);  
  70.         //动态规划递推关系  
  71.         for(int j=2; j<=n; j++){  
  72.           for(int k=1; k<j; k++){  
  73.             f[j][k]=maxnum((f[j-1][k-1]+score(n-k+1,j)),(f[j-1][k]+score(j-k,j)));  
  74.           }  
  75.         }  
  76.         //得到收益最大并记录  
  77.         max = f[n][0];  
  78.         for(int g=1; g<=n; g++){  
  79.             if(f[n][g]>max)  
  80.               max=f[n][g];  
  81.         }  
  82.         printf("%d\n",max*200);  
  83.     }  
  84. }  
  85. </span>  

贪心算法

这个贪心算法确实比较难想,可以参考poj上面的大牛http://poj.org/showmessage?message_id=164719,摘录如下:

贪心的本质在于:田只在有把握赢的情况下拿出快马和王拼,否则用最慢的马比掉王的快马最大程度削弱王的战斗力

*
贪心策略:
1,如果田忌的最快马快于齐王的最快马,则两者比。
(因为若是田忌的别的马很可能就赢不了了,所以两者比)
2,如果田忌的最快马慢于齐王的最快马,则用田忌的最慢马和齐王的最快马比。
(由于所有的马都赢不了齐王的最快马,所以用损失最小的,拿最慢的和他比)
3,若相等,则比较田忌的最慢马和齐王的最慢马
3.1,若田忌最慢马快于齐王最慢马,两者比。
(田忌的最慢马既然能赢一个就赢呗,而且齐王的最慢马肯定也得有个和他比,所以选最小的比他快得。)
3.2,其他,则拿田忌的最慢马和齐王的最快马比。
(反正所有的马都比田忌的最慢马快了,所以这匹马必输,选贡献最大的,干掉齐王的最快马)

[cpp]  view plain copy
  1. #include<cstdio>  
  2. #include<cstdlib>  
  3. #include<cstring>  
  4. #include<cmath>  
  5. #include<algorithm>  
  6. using namespace std;  
  7. const int maxn=1005;  
  8.   
  9. int tj[maxn], qw[maxn];  
  10.   
  11. int main()  
  12. {  
  13.     int n, i, res, max1, max2, min1, min2, cnt;  
  14.     while(~scanf("%d", &n) && n)  
  15.     {  
  16.         for(i=0; i<n; i++)  
  17.             scanf("%d", &tj[i]);  
  18.         for(i=0; i<n; i++)  
  19.             scanf("%d", &qw[i]);  
  20.         sort(tj, tj+n);  
  21.         sort(qw, qw+n);  
  22.   
  23.         res=0;  
  24.         max1=max2=n-1;  
  25.         min1=min2=0;  
  26.         cnt=0;  
  27.         while((cnt++)<n)  
  28.         {  
  29.             if(tj[max1]>qw[max2])  
  30.             {  
  31.                 res += 200;  
  32.                 max1--;  
  33.                 max2--;  
  34.             }  
  35.             else if(tj[max1]<qw[max2])  
  36.             {  
  37.                 res -= 200;  
  38.                 min1++;  
  39.                 max2--;  
  40.             }  
  41.             else  
  42.             {  
  43.                 if(tj[min1]>qw[min2])  
  44.                 {  
  45.                     res += 200;  
  46.                     min1++;  
  47.                     min2++;  
  48.                 }  
  49.                 else  
  50.                 {  
  51.                     if(tj[min1]<qw[max2]) res -= 200;  
  52.                     min1++;  
  53.                     max2--;  
  54.                 }  
  55.             }  
  56.         }  
  57.         printf("%d\n", res);  
  58.     }  
  59.     return 0;  
  60. }  

你可能感兴趣的:(田忌赛马(Tian Ji -- The Horse Racing)中的动态规划以及贪心算法)