杭电oj1052题:Tian Ji -- The Horse Racing

来源:互联网 发布:mac怎么用wifi万能钥匙 编辑:程序博客网 时间:2024/06/05 12:45
题目: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就不用改变。

若田忌最差的马比齐王最差的马还差的话,那个我们直接拉去和齐王最强的马进行比较就好了,消耗齐王最强的马。

原创粉丝点击