Tian Ji -- The Horse Racing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17221 Accepted Submission(s): 4998
"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.
引用了古代田忌赛马的故事,不过此题将这个故事的三马,扩充到了n配马,面对这样一个比赛,解题方法多样。
方法一:
我们不妨先让这些马按从小到大的顺序排序,比如样列中的数据一:
/*coder hdu 1055@Gxjun*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int aa[*maxn],bb[maxn];
int main()
{
int n,i,j,count,cnt;
while(scanf("%d",&n)!=EOF&&!=n){
for(i=;i<n;i++) scanf("%d",aa+i);
for(i=;i<n;i++) scanf("%d",bb+i);
sort(aa,aa+n); //从大到小排序 general
sort(bb,bb+n); //从小到大排序 king
for (i=;i<n;i++) aa[i+n]=aa[i];
count=-0x3f3f3f3f;
for(i=;i<n;i++){
cnt=;
for(j=;j<n;j++){
if(aa[i+j]<bb[j]) cnt--;
else if(aa[i+j]>bb[j]) cnt++;
}
if(count<cnt) count=cnt;
}
printf("%d\n",count*);
}
return ;
}
对于这道题可以用二分匹配,但是tle ,面对这一个问题,我们还可以使用贪心什么的求解,最终得到我们所需要的答案!
反正总之一句话,如果田忌的慢马比国王的慢马快,直接比较,否则这个慢马
毫无价值,用来当炮灰,去跟国王的快马相比,如果田忌的快马比国王快,直接比较
否则这个快马也是个炮灰,只好去跟国王后边的慢马比,但是到底跟那个比可不一定,
所以不用去一个一个的试,当快马比不过的时候,用慢马去当炮灰,然后用快马跟国王
的下一匹快马比较