程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu1052——Tian Ji -- The Horse Racing

hdu1052——Tian Ji -- The Horse Racing

編輯:C++入門知識

hdu1052——Tian Ji -- The Horse Racing


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

\

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 hZ喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcnNlIHJhY2luZyBwcm9ibGVtIGNhbiBiZSBzaW1wbHkgdmlld2VkIGFzIGZpbmRpbmcgdGhlIG1heGltdW0gbWF0Y2hpbmcgaW4gYSBiaXBhcnRpdGUgZ3JhcGguIERyYXcgVGlhbg=="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 | 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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved