1. 程式人生 > >杭電oj1052題:Tian Ji -- The Horse Racing

杭電oj1052題:Tian Ji -- The Horse Racing

題目: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 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。

先給出程式碼:

#include<iostream>
#include<vector>

using namespace std;
void sort(vector<int>& vec,int size)
{
	bool flag=false;
	while(!flag)
	{
		flag=true;
		for(int i=0;i<size-1;i++)
		{
			if(vec[i]<vec[i+1])
			{
				flag=false;
				int temp=vec[i];
				vec[i]=vec[i+1];
				vec[i+1]=temp;
			}
		}
		size--;
	}
}
int main()
{
	int n;
	while(cin>>n,n)
	{
		vector<int> tian;
		vector<int> king;
		for(int i=0;i<n;i++)
		{
			int temp;
			cin>>temp;
			tian.push_back(temp);
		}
		for(int i=0;i<n;i++)
		{
			int temp;
			cin>>temp;
			king.push_back(temp);
		}
		sort(tian,n);
		sort(king,n);
		int count=0;
		int tian_max=0;
		int tian_min=n-1;
		int king_max=0;
		int king_min=n-1;
		while(tian_max<=tian_min&&king_max<=king_min) 
		{
			if(tian[tian_min]>king[king_min])
			{
				tian_min--;
				king_min--;
				count++;
			}
			else if(tian[tian_min]<king[king_min])
			{
				tian_min--;
				king_max++;
				count--;
			}
			else
			{
				if(tian[tian_max]>king[king_max])
				{
					tian_max++;
					king_max++;
					count++;
				}
				else if(tian[tian_max]<king[king_max])
				{
					tian_min--;
					king_max++;
					count--;
				}
				else
				{
					if(tian[tian_min]==king[king_max])
					{
						tian_min--;
						king_max++;
					} 
					else
					{
						tian_min--;
						king_max++;
						count--;
					}
					
				}
			}
		}
		count=count*200;
		cout<<count<<endl;
	}
	return 0;
}

題目的主要思想是田忌賽馬這個背景下的一個問題,就是兩隊馬,馬有好又壞,問我們如何能贏的把數最多。顯然這個問題我們可以用貪心的想法來解決這個問題。

顯然這個貪心規則應該滿足這麼兩點:

1.贏得話要用最小的代價贏;

2.輸的話用輸給對面最大的馬來輸。

所以對於這個問題,我是從田忌最差的馬可以這個問題,若田忌最差的馬比齊王快,那麼我們最讓他們兩個比,這個時候顯然滿足貪心的規則1;

若田忌最差的馬與齊王的相等那麼我們需要先比較雙方最強的馬,若田忌的強則此輪用田忌此輪最強的馬與齊王此輪最強的馬進行比,否則的話我們就用前面最差的馬消耗齊王最強的馬。當然我們要注意,可能會出現齊王最強的馬可能和我們最差的馬一樣強,這樣的話count就不用改變。

若田忌最差的馬比齊王最差的馬還差的話,那個我們直接拉去和齊王最強的馬進行比較就好了,消耗齊王最強的馬。

相關推薦

oj1052Tian Ji -- The Horse Racing

題目: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 li

hdu 1052 Tian Ji -- The Horse Racing 可惡的貪心-------也算是經典貪心吧,對於一般人來說,不看題解,應該很難做出來吧

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 ra

poj 2287 Tian Ji -- The Horse Racing

!= space 好的 賽馬 content main 題意 一場 void poj 2287 Tian Ji -- The Horse Racing 題意: 田忌賽馬 假設3匹馬變成1000匹。齊王仍然讓他的馬按從優到劣的順序出賽,田忌能夠按隨意順序選

POJ 2287 - Tian Ji -- The Horse Racing(田忌賽馬) 題解

ons for amp 匹配 void 代碼 div line 比賽 此文為博主原創題解,轉載時請通知博主,並把原文鏈接放在正文醒目位置。 題目鏈接:http://poj.org/problem?id=2287 題目大意: 田忌賽馬的故事就不用多說了吧.... 輸

HDU 1052 Tian Ji -- The Horse Racing

分享圖片 open sil inner sig ber trick red tdi Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276

Tian Ji -- The Horse Racing

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

hdu 1052 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): 37

HDU 1052 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): 11

hdu 1052 Tian Ji -- The Horse Racing

題目真的是有點長。 題意: 兩組相同數量的數字,兩兩對比不能重複使用,如果第一組的一個大於第二組的一個則加200分,小於減200分,等於不加不減。 思路: 先將兩組都按照從小到大進行排列,貪心的思路記下來第一組比第二組大的數量。具體就是,當成兩個指標,分別表示當前比較

3266 Tian Ji -- The Horse Racing

題目傳送門 #include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <algorithm> #in

HDOJ-1052 Tian Ji -- The Horse Racing(貪心演算法)

題目描述:就是田忌賽馬,但是要注意兩邊存在馬速度相同的情況。 分析: (看了各路大佬的題解Orz,自我總結一下) 先排序,這裡從快到慢排序; 1.田忌最快馬>王最快馬(貪心,毫無疑問,進行比賽後獲勝) 2.田忌最快馬<王最快馬(同樣的,既然贏不了

2018北大暑校acm演算法訓練課程 Tian Ji -- The Horse Racing 貪心

總時間限制: 5000ms 記憶體限制: 65536kB 描述 Here is a famous story in Chinese history. That was about 2300 years ago. General Tian Ji was a

POJ 2287 Tian Ji -- The Horse Racing&&浙江科技學院第十三屆程式設計競賽1006 田忌賽馬後傳(貪心)

思路:如果田忌最慢的比齊王最慢的快,或者田忌最快的比齊王最快的快,那麼就比,否則讓田忌最慢的和齊王最快的比。 #include<map> #include<queue> #in

【貪心】HDU 1052 Tian Ji -- The Horse Racing n匹馬的“田忌賽馬”

題意:田忌賽馬。田忌和齊王各有n匹馬,輸入田忌的馬的速度和齊王的馬的速度。每一輪田忌贏了就得200兩銀子,平就得0兩,輸了就失去200兩銀子。問田忌最多能得到多少。題目的策略是貪心,分析見leokan大牛的blog: http://hi.baidu.com/leokan/b

Tian Ji -- The Horse Racing(田忌賽馬模擬)+貪心

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 ra

1052 Tian Ji -- The Horse Racing

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 li

LA 3266&HDU 1052 Tian Ji -- The Horse Racing(田忌賽馬,貪心)

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 r

HDU-1052 POJ-2287 Tian Ji -- The Horse Racing(田忌賽馬)

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 ra

UVa LA 3266 - Tian Ji -- The Horse Racing 貪心,不只處理一端,也處理另一端以理清局面 難度: 2

優先 i++ pen repos repo esp 解決 無法 pos 題目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr

hdu1052Tian 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): 37