码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1052——Tian Ji -- The Horse Racing

时间:2014-11-28 18:20:11      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:贪心

Tian Ji -- The Horse Racing

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


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

bubuko.com,布布扣

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
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1050 2037 1045 1800 1257 
 

Statistic | Submit | Discuss | Note


贪心

      如果田忌最快的马比国王最快的马要快,那么直接比

      如果田忌最快的马比国王最快的马要慢,那么用田忌最慢的马去和国王最快的马比,(反正一定要输一场,而田忌最快的马可能还能在其他比赛中胜出,所以用田忌最慢的去比至少不会吃亏)

      如果田忌最快的马和国王最快的马一样快,那么要分情况

      A.如果田忌最慢的马的速度小于国王最慢的马,那么就用田忌最慢的马去和国王现在最快的马比(因为田忌最慢的马现在一场也赢不了,所以还不如来抵抗国王最快的,留下自己最快的可能还能在其他比赛中胜出)

      B.如果田忌最慢的马速度和国王最慢的马的速度一样,那么此时,我们还是应该用这匹最慢的马去和国王最快的马比(如果是快对快,慢对慢这样比,虽然不会亏,但是田忌最快的马已经用掉,不可能在去盈利,而如果是用田忌最慢的马去和国王最快的马比,虽然这里可能吃亏了,但是存在一种情况,即使田忌最快的马在面对除国王最慢以外的其他马时,无法盈利,但是此时田忌最快的马面对国王最慢的马时,至少不会吃亏,所以2种策略对比,后者更可取,因为除上述情况外, 田忌还是可能获利的)

       C. 如果田忌最慢的马的速度大于国王最慢的马,那么就应该用田忌最慢的马去和国王最慢的马比(这里至少可以获利,去除所有C的情况,我们把情况转化为A,B,即使采取A,B策略亏本了(面对国王最快的马然后输掉比赛),C策略下的盈利还是大于等于亏本的)


#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1010;
int tianji[N], king[N];

int main()
{
	int n;
	while (~scanf("%d", &n), n)
	{
		for (int i = 0; i < n; ++i)
		{
			scanf("%d", &tianji[i]);
		}
		for (int i = 0; i < n; ++i)
		{
			scanf("%d", &king[i]);
		}
		sort(tianji, tianji + n);
		sort(king, king + n);
		int king_max = n - 1, tianji_min = 0, king_min = 0;
		int ans = 0, i = n - 1;
		while (i >= tianji_min && king_max >= king_min)
		{
			if (tianji[i] > king[king_max])
			{
				ans += 200;
				king_max--;
				i--;
			}
			else if (tianji[i] < king[king_max])
			{
				king_max--;
				tianji_min++;
				ans -= 200;
			}
			else
			{
				if (tianji[tianji_min] < king[king_min])
				{
					king_max--;
					tianji_min++;
					ans -= 200;
				}
				else
				{
					for (int j = tianji_min; j < n; ++j)
					{
						if (tianji[j] > king[king_min])
						{
							ans += 200;
							king_min++;
						}
						else
						{
							tianji_min = j;
							if (tianji[tianji_min] < king[king_max])
							{
								ans -= 200;
							}
							king_max--;
							tianji_min++;
							break;
						}
					}
				}

			}
		}
		printf("%d\n", ans);
	}
	return 0;
}


hdu1052——Tian Ji -- The Horse Racing

标签:贪心

原文地址:http://blog.csdn.net/guard_mine/article/details/41577667

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!