HDOJ--1052Tian Ji -- The Horse Racing 贪心!!!

本文解析了经典的田忌赛马问题,通过贪心算法解决如何最大化收益的问题,并提供了详细的算法思路及实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接

ProblemDescription

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 maximum weightedperfect 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.

 

 

Input

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.

 

 

Output

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

 

 

Sample Input

3

9283 71

9587 74

2

2020

2020

2

2019

2218

0

 

 

Sample Output

200

0

0

 

   田忌赛马问题:经典田忌赛马问题,给你几匹马,每次比赛输方给获胜方200silverdollars,平局不给。

此题田忌的马可以比国王的更好,田忌也可以拥有跟国王相同的马匹。

   思想:贪心!(以下用T代表田忌,用K代表国王)

.T最慢的马比K,即田忌赢下所有比赛

.K最慢的马比T,即田忌输掉所有比赛

.其他情况比较

1.T最快的马比K最快的慢,用T最慢输K最快,可以保证田忌最快的马赢可以赢得下一局

2.T最快的马比K快,用T最快赢K最快,田忌最快的马比国王快,先赢下来,不必要用T最慢刻意输K最快

3.最快和K最快相等(此处分类讨论)

T最慢比K最慢快,用T最慢跑K最慢 

T最慢比K最慢慢,用T最慢跑K最快 

T最慢和K最慢一样快,比较如果T最慢比K最快慢,用T最慢跑K最慢,

否则,另一种情况是T最慢和K最快相等,平局,最T最慢跑K最快

 

代码:


#include<stdio.h>
#include<algorithm>

using namespace std;

#define horsenumber 1001 //题目最大马匹为1000 
#define silverdollars 200//每次赢或输的金钱 

int Tian[horsenumber];
int King[horsenumber];

bool cmp(int a,int b)//让sort函数从大到小排序 
{
	return a>b;
}

int main()
{
	int i,n,s;
	int t_begin,k_begin,t_end,k_end;//t_begin:T目前最快的马,t_end:T目前最慢的马,k_begin,k_end类推 
	while(scanf("%d",&n)&&n)
	{
		for(i=0;i<n;i++)
		{
			scanf("%d",&Tian[i]);
		}
		for(i=0;i<n;i++)
		{
			scanf("%d",&King[i]);
		}
		sort(Tian,Tian+n,cmp); //从大到小排序 
		sort(King,King+n,cmp);
		
		s=0;                   //T最慢的马比K快 
		if(Tian[n-1]>King[0])    {
			s=n;
		}
		else if(King[n-1]>Tian[0]){//K最慢的马比T快 
			s=-n;
		}
		else{                      //其他情况比较 
			t_begin=k_begin=0;
			t_end=k_end=n-1;
			for(i=0;i<n;i++)
			{
				if(Tian[t_begin]<King[k_begin])//T最快的马比K最快的慢,用T最慢输K最快 
				{
					if(Tian[t_end]<King[k_begin])
					s--;
					t_end--;
					k_begin++;
				}
				else if(Tian[t_begin]>King[k_begin])//T最快的马比K快,用T最快赢K最快 
				{
					s++;
					t_begin++;
					k_begin++;
				}
				else{                        //T最快和K最快相等
					if(Tian[t_end]>King[k_end])//T最慢比K最慢快,用T最慢跑K最慢 
					{
						s++;
						t_end--;
						k_end--;
					}
					else if(Tian[t_end]<King[k_end])//T最慢比K最慢 慢,用T最慢跑K最快 
					{
						s--;
						t_end--;
						k_begin++;
					}
					else{           //T最慢和K最慢一样快,比较如果T最慢比K最快慢,用T最慢跑K最慢 
						if(Tian[t_end]<King[k_begin])
						{
							s--;
							t_end--;
							k_begin++;
						 } 
						 else{		//另一种情况是T最慢和K最快相等,谁都不赢,最T最慢跑K最快 
						 	t_end--;
							k_begin++;
						 }
					}
				}
			}
		}
		printf("%d\n",s*silverdollars);
	}
	
}



测试数据:

------

3

2 3 5

3 4 4

 

3

5 8 11

5 10 15

 

3

92 83 70

92 82 60

 

3

92 83 71

95 92 74

 

 

3

92 83 70

92 91 60

 

2

1 2

1 3

 

8

11 9 8 8 8 4 3 2

11 8 8 8 8 4 3 2

 

3

93 83 71

95 87 74

 

2

20 20

20 20

 

2

20 19

22 18

 

2

20 19

20 21

 

2

20 21

20 19

 

10

12 3 4 5 23 5 23 6 12 8

23 6 7 8 8 9 23 4 6 3

 

3

1 2 3

1 2 3

 

0

 

------

答案

------

 

0   200  400  0

200 0    800  200

0   0   -200  400

1000 200

测试数据转自:http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=29058&messageid=1&deep=0

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值