Tian Ji -- The Horse Racing
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 6507 | Accepted: 2002 |
Description
Here is a famous story in Chinese history.
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.
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.
Input
The 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.
Output
For 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
中国古代的历史故事“田忌赛马”是为大家所熟知的。话说齐王和田忌又要赛马了,他们各派出N匹马,每场比赛,输的一方将要给赢的
一方200两黄金,如果是平局的话,双方都不必拿出钱。现在每匹马的速度值是固定而且已知的,而齐王出马也不管田忌的出马顺序。请
问田忌该如何安排自己的马去对抗齐王的马,才能赢取最多的钱?
第一行为一个正整数n (n <= 1000) ,表示双方马的数量。
第二行有N个整数表示田忌的马的速度。
第三行的N个整数为齐王的马的速度。
每组数据代表马奔跑的速度,第一行是马的数量,第二行是田忌的马的速度,第二行是齐威王的马的速度。
思想是这样的,先把各组马的速度从大到小排序,然后用田忌的马顺序与齐威王的马比较
if(田忌的马快)
比较下一对马;
else if(田忌的马慢)
用田忌最慢的马和齐威王的这匹马赛
else
{
从未进行比赛的速度小的马开始从后往前比
if(田忌的马快)
继续往前比
else
用这匹马和刚才跑平的齐威王的马比
//总之原则就是如果这匹马不能赢,就让他和比他快很多的马比,这样保持速度较快的马
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
bool cmp(int x,int y){return x>y;}
int main()
{
int a[1100],b[1100],n,res[1100];//a 田忌 b 齐王
while(scanf("%d",&n)==1&&n)
{
int cnt=0;
for(int i=0;i<n;i++) scanf("%d",&a[i]);
for(int i=0;i<n;i++) scanf("%d",&b[i]);
sort(a,a+n,cmp);//j 从大到小 田忌
sort(b,b+n,cmp);//i 从大到小 齐王
int fin_t=n-1,fin_q=n-1;//田忌 齐王
for(int i=0,j=0;i<n&&j<=fin_t;i++)
{
if(a[j]>b[i])
{
res[i]=j;
j++;
cnt+=200;
}
else if(a[j]<b[i])
{
fin_t--;
cnt-=200;
}
else
{
for(int m=fin_q,k=fin_t;k>=j;m--,k--)
{
if(a[k]>b[m])
{
cnt+=200;
fin_q--,fin_t--;
}
else
{
if(a[k]<b[i]) cnt-=200;
fin_t=k-1;
fin_q=m;
break;
}
}
}
}
printf("%d/n",cnt);
}
return 0;
}