"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.
3 92 83 71 95 87 74 2 20 20 20 20 2 20 19 22 18 0
200 0 0
田忌赛马
题意:输入两组数据,用类似于田忌赛马的方式比较第一组数据最多可以赢多少局,赢一局获得200金,输一局赔200金,打平不输不赢。
思路:
将两组数据按从小达大的顺序排列,将其分别压入vector数组中,然后对其进行比较;
先比较最小的,如田的马大于国王的马,从vector删除,计num+1;否则在比较最大的,如果田的大于国王的,从vector删除,num+1;
若田 的最小的最大的都没比过,用田的最小的比掉国王的 最大的。依次比较直至vector为空。
这道题,错了好多次,总是少 分析了
输入:
6
88 92 92 99 120 130
92 92 99 100 110 120
2
9 5
9 2
输出:
200
200
若上面两组数据输出对的话,那么这道题就差不多了。
代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;
int main()
{
int N;
while(1)
{
cin>>N;
if(N==0)
break;
int a[1001],b[1001];
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=0;i<N;i++)
cin>>a[i];
for(int i=0;i<N;i++)
cin>>b[i];
sort(a,a+N);
sort(b,b+N);
vector<int>vec1(a,a+N);
vector<int>vec2(b,b+N);
vector<int>::iterator it1,it2;
int num=0,num2=0;
for(it1=vec1.begin();it1!=vec1.end();) //若vector的删除操作及其返回值的问题有疑惑,自己百度,
{
for(it2=vec2.begin();it2!=vec2.end();it2++)
{
if((*it1)>(*it2))
{
it2=vec2.erase(it2);
it1=vec1.erase(it1);
num++;
break;
}
else
{
if(*(vec1.end()-1)>*(vec2.end()-1))
{
num++;
vec1.erase(vec1.end()-1);
vec2.erase(vec2.end()-1);
break;
}
if((*it1)==*(vec2.end()-1))
{
vec2.erase(vec2.end()-1);
it1=vec1.erase(it1);
break;
}
else
{
num2++;
vec2.erase(vec2.end()-1);
it1=vec1.erase(it1);
break;
}
}
}
}
cout<<(num-num2)*200<<endl;
}
system("pause");
return 0;
}