贪心算法 003:Tian Ji -- The Horse Racing
003: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 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.
輸入
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.
輸出
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.
樣例輸入
樣例輸出
200 0 0思路:
tianji的馬從小到大排序——國王的馬從小到大排序
該題難點在于要分析出所有情況
小馬的比較
(1)當T小馬大于K小馬時——錢++,比較兩邊第二個小馬;
(2)當T小馬小于K小馬時——必輸,不如用小馬碰大馬,錢–;
(3)當T小馬與K小馬相等時——需要先比較大馬
因為:
可能小馬相同,Tian的大馬全部大于King的大馬,這時無需讓小馬碰大馬,徒增敗績,讓小馬直接碰掉小馬就行了
大馬的比較
(1)當T大馬大于K大馬時——錢++,比較兩邊第二大馬;
(2)當T大馬小于K大馬時——必輸,讓T小馬碰K大馬,錢–;
(3)當T大馬等于K大馬且T小馬=K小馬時——還是小馬碰大馬
如果小馬小于對方大馬,則錢–,不然錢不變;
一個錯誤:
1)認為最快的兩個馬一樣快,就碰掉
2) 在最快馬相同時,認為最慢的兩個馬一樣快,就碰掉 ****。其實還是應該拿最慢馬去和對方最快馬比。考慮兩個序列完全相同的情況
3) 沒考慮到最后剩下兩個馬一樣快的情況
4) 在最快馬等速時,認為自己的尾馬一定比對方的頭馬慢
問題的關鍵是,頭馬一樣快,且尾馬也一樣快,則用自己尾馬對對方頭馬。證明:替換法。
假設最優法是兩個尾馬兌掉,在這種情況下的最佳做法里面,對方的頭馬a,對的是我的馬x
那么我可以維持其它對局不變,用我的尾馬 z對a, x對對方的尾馬b (z=b)
若a>x ,則變換后,a還是贏的,但我方原來輸掉的x >= b,變換后的結果不會更差
若a=x, 則變換后, a對z, x 對b。
若a>z,則 x > b (因z=x) ,還是扯平,沒有變得更差
若a = z,則x=b,還是扯平,也沒有變得更差
總結
以上是生活随笔為你收集整理的贪心算法 003:Tian Ji -- The Horse Racing的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大数据在5G应用场景下有哪些示范项目?
- 下一篇: MongoDB分片存储集群支撑海量数据