杭电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就不用改變。
若田忌最差的馬比齊王最差的馬還差的話,那個我們直接拉去和齊王最強的馬進行比較就好了,消耗齊王最強的馬。
總結
以上是生活随笔為你收集整理的杭电oj1052题:Tian Ji -- The Horse Racing的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022年苏州市重点软件企业和专精特新软
- 下一篇: js怎样判断是不是整数