hdu 1052 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): 13412    Accepted Submission(s): 3775


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

hdu 1052 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

 

题意:田忌赛马......问给出两列马的战斗力,问田忌最多能赚多少。

 

题解:明显贪心了,一开始自己贪心策略想简单了,想得是尽快将能将就吧大王的马赢了的就尽管赢,赢不了的马就拿去抵掉大王的那些很牛逼的马。实际上基本策略是这样贪的。

 

 

 

但根据几组实验的测试数据要注意:

一、

2
1 2
1 3

若最慢的马相等,看最快的马如果最快的马田忌比不了大王的,就用最慢的田忌马去抵掉大王的最快马

 

二、

 9
80 140 76 58 93 140 146 74 173
97 80 135 72 0 107 155 66 141

若最慢的马相等,若最快的马田忌的比大王的快,那么就让最快马比,接着放空这组最慢相等马,继续从最快比。注意这里的最慢最快都是递归的,因为,一旦一对马比完就被淘汰了,不能参加两次比赛,这样最慢 最快的马会变成另外的。所以这里的最慢最快都是递归的。

 

 

所以综合这两种测试数据 得出完整的贪心策略:(把马从大到小排序)

先从最慢的马依次比:1、田忌最慢马比大王最慢马快,直接比赛

                                        2、田忌最慢马比大王最慢马慢,让田忌最慢马与大王最快马比赛(反正它都是输,就让它输得更有价值一点,把对面的一个强劲的对手pass掉)

                                        3、田忌最慢马与大王最慢马相等:(这时放空最慢马,从最快马来讨论)

                                                            (1)、田忌最快马比大王最快马快,直接比赛

                                                            (2)、田忌最快马比大王最快马慢,让刚才的田忌最慢马与大王最快马比赛

                                                            (3)、田忌最快马与大王最快马相等,让刚才的田忌最慢马与大王最快马比赛

按照这样的策略将所有马都比一次就是田忌能赚最多的钱了,即最优方案。

 

关于贪心策略的证明:(还没想好,等想好再写 标记下###########################################)

 

代码:

/*
acm
hdu:Tian Ji -- The Horse Racing


the test data
2
1 2
1 3
 will let my program produce the WA
 
 
 9
80 140 76 58 93 140 146 74 173
97 80 135 72 0 107 155 66 141

this data also make it be WA

*/
#include<stdio.h>
#include<stdlib.h>

int n=0;

int Tian[1000+5]={0},King[1000+5]={0};

int money=0;

/*compare for the qsort*/
int qcmp(const void * a,const void * b)
{
	int * A=(int *)a;
	int * B=(int *)b;
	return(*B-*A);//from big to small
}

/*for test*/
int test()
{
	return(0);
}

/*main process*/
int MainProc()
{
	while(scanf("%d",&n)!=EOF&&n>0)
	{
		int i=0;
		money=0;
		for(i=0;i<=n-1;i++)
		{
			scanf("%d",&Tian[i]);
		}
		qsort(Tian,n,sizeof(Tian[0]),qcmp);
		for(i=0;i<=n-1;i++)
		{
			scanf("%d",&King[i]);
		}
		qsort(King,n,sizeof(King[0]),qcmp);
		int ti=n-1,ki=n-1,kj=0,tj=0;
		while(ti>=tj)
		{
			if(Tian[ti]>King[ki])
			{
				ki--;
				ti--;
				money+=200;
			}
			else if(Tian[ti]<King[ki])
			{
				money-=200;
				kj++;//the bigest has been matched
				ti--;
			}
			else//if the result is equal it may not come to a match
			{
				if(Tian[tj]>King[kj])
				{
					tj++;
					kj++;
					money+=200;
				}
				else if(Tian[tj]<King[kj])
				{
					if(Tian[ti]<King[kj])
					{
						money-=200;
					}
					ti--;
					kj++;
				}
				else
				{
					if(Tian[ti]<King[kj])
					{
						money-=200;
					}
					ti--;
					kj++;
				}
			}
		}
		printf("%d\n",money);
	}
	return(0);
}

int main()
{
	MainProc();
	return(0);
}


 

 

 

 

 

你可能感兴趣的:(hdu 1052 Tian Ji -- The Horse Racing)