U - 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.
InputThe 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.
OutputFor each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.
Sample Input
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
0
Sample Output
200
0
0
思路:(1)先比较两者的好马,如果田忌的好马可以赢过齐王的好马,果断去比,高手过招,赢了才更有满
足感,自豪感嘛!
(2)再比较不好的马,如果田不好的马可以赢过齐王的不好的马,毫不犹豫去比,反正可以赢。用田
忌最弱的战胜对方最弱的,这才符合江湖规矩。
(3)如果以上都不成立,那么如果田的坏马比齐王的坏马慢,或者速度一样,就用田的坏马去和齐王
的好马比,拉齐王的好马下水,两个坏马的速度一样时,与其平局,不如让田的坏马拉齐王好马
下水。
(4)好马坏马,速度大小都是相对的,不绝对。
方法一:贪心
#include
#include
using namespace std;
bool cmp(int A,int B)
{
return A>B;
}
int t[1000+22];
int q[1000+22];
int main()
{
int n,num,t_max,t_min,q_max,q_min,i;
while(scanf("%d",&n)!=EOF&&n)
{
num=0;
for(i=0;iq[q_max])
{
num++;
t_max++;
q_max++;
}
else if(t[t_min]>q[q_min])
{
num++;
t_min--;
q_min--;
}
else
{
if(t[t_min]
方法二:双端队列
#include
#include
#include
#include
using namespace std;
int main()
{
int n,num,i,t,v1,v2;
while(~scanf("%d",&n)&&n)
{
num=0;
deque q1;
deque q2;
for(i=1;i<=n;i++)
{
scanf("%d",&t);
q1.push_back(t); //表示从队尾插入数据
}
for(i=1;i<=n;i++)
{
scanf("%d",&t);
q2.push_back(t);
}
sort(q1.begin(),q1.end());//双端队列从小到大排序,两者分别表示队列的首末地址
sort(q2.begin(),q2.end());
for(i=1;i<=n;i++)
{
if(q1.back()>q2.back())//比较队尾,队尾是好马,好马可以赢,果断去比
{
num+=200;
q1.pop_back();//删除队尾元素,并不返回其值
q2.pop_back();
continue;//比完一组好马后,接着比下一组好马,不用进行下面内容
}
v1=q1.front();
v2=q2.front();
if(v1>v2)//比较队首,队首是笨马,笨马可以赢,果断去比
{
num+=200;
q1.pop_front();//删除队首元素,不返回其值
q2.pop_front();
}
else
{ //田忌笨马和齐王好马比,拉齐王下水,
if(q1.front()!=q2.back())//速度不一样,就是比人家低,输了
num-=200;
q1.pop_front();
q2.pop_back();
}
}
printf("%d\n",num);
}
return 0;
}
方法三:还是双端队列
#include
#include
#include
#include
using namespace std;
int cmp(int A,int B)
{
return A>B;
}
int main()
{
int n,num,i,t,v1,v2;
while(~scanf("%d",&n)&&n)
{
num=0;
deque q1;
deque q2;
for(i=1;i<=n;i++)
{
scanf("%d",&t);
q1.push_front(t); //表示从队首插入数据
}
for(i=1;i<=n;i++)
{
scanf("%d",&t);
q2.push_front(t);
}
sort(q1.begin(),q1.end(),cmp);//双端队列从大到小排序,两者分别表示队列的首末地址
sort(q2.begin(),q2.end(),cmp);
for(i=1;i<=n;i++)
{
if(q1.front()>q2.front())//比较队首,队首是好马,好马可以赢,果断去比
{
num+=200;
q1.pop_front();//删除队首元素,并不返回其值
q2.pop_front();
continue;//比完一组好马后,接着比下一组好马,不用进行下面内容
}
v1=q1.back();
v2=q2.back();
if(v1>v2)//比较队尾,队尾是笨马,笨马可以赢,果断去比
{
num+=200;
q1.pop_back();//删除队尾元素,不返回其值
q2.pop_back();
}
else
{ //田忌笨马和齐王好马比,拉齐王下水,
if(q1.back()!=q2.front())//速度不一样,就是比人家低,输了
num-=200;
q1.pop_back();
q2.pop_front();
}
}
printf("%d\n",num);
}
return 0;
}