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<cstdio>
-
#include<algorithm>
-
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;i<n;i++)
-
scanf("%d",&t[i]);
-
for(i=0;i<n;i++)
-
scanf("%d",&q[i]);
-
sort(t,t+n,cmp);
-
sort(q,q+n,cmp);//两组马速度从大到小排序
-
t_max=0;
-
t_min=n-1;
-
q_max=0;
-
q_min=n-1; //代表速度最大最小的下标,比完一组,换下一组
-
while(t_max<=t_min&&q_max<=q_min)
-
{
-
if(t[t_max]>q[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]<q[q_max])
-
num--;
-
t_min--;
-
q_max++;
-
}
-
}
-
printf("%d\n",200*num);
-
}
-
return 0;
-
}
方法二:双端队列
-
#include<cstdio>
-
#include<queue>
-
#include<deque>
-
#include<algorithm>
-
using namespace std;
-
int main()
-
{
-
int n,num,i,t,v1,v2;
-
while(~scanf("%d",&n)&&n)
-
{
-
num=0;
-
deque<int> q1;
-
deque<int> 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<cstdio>
-
#include<queue>
-
#include<deque>
-
#include<algorithm>
-
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<int> q1;
-
deque<int> 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;
-
}
嗯
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
using namespace std;
const int maxn=1e3+10;
int dp[maxn][maxn];//dp[i][j],打了i场,用了j匹慢马
int a[maxn],b[maxn];
int judge(int i,int j)
{
if(a[i]>b[j])return 1;
else if(a[i]<b[j])return -1;
else return 0;
}
int cmp(int x,int y)
{
return x>y;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
int i,j,k;
for(i=0;i<n;i++)scanf("%d",&a[i]);
for(i=0;i<n;i++)scanf("%d",&b[i]);
sort(a,a+n);
sort(b,b+n,cmp);
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
if(a[n-i]>b[i-1])dp[i][0]=dp[i-1][0]+1;
else if(a[n-i]<b[i-1])dp[i][0]=dp[i-1][0]-1;
else dp[i][0]=dp[i-1][0];
if(a[i-1]>b[i-1])dp[i][i]=dp[i-1][i-1]+1;
else if(a[i-1]<b[i-1])dp[i][i]=dp[i-1][i-1]-1;
else dp[i][i]=dp[i-1][i-1];
}
for(i=2;i<=n;i++)
{
for(j=1;j<i;j++)
{
//printf("*%d %d %d\n",i,j,j);
dp[i][j]=max(dp[i-1][j]+judge(n-i+j,i-1),dp[i-1][j-1]+judge(j-1,i-1));
}
}
int ans=-1000;
for(i=0;i<=n;i++)
ans=max(ans,dp[n][i]);
printf("%d\n",ans*200);
}
return 0;
}
/*
3
2 3 5
3 4 4
4
1 2 3 4
2 3 4 5
6
2 2 3 3 4 4
2 2 3 3 4 4
*/
题解:
1)dp
最开始想用贪心,但后来发现在有很多相等速度马的情况下,很难贪心出来。所以需要用dp。
dp[i][j]表示打了i场比赛,用了j匹慢马(从最慢的开始用)和i-j匹快马(从最快的开始用)时的最优成绩。
原理:也算是一种变着法的贪心把。我们将国王的马从大到小排序,我们要么用最好的马战胜掉,要么用最差的马顶掉。
若不用最差的马还是输了,那最差的马还是要输给别的马,当然用最差的马好;若没有输,那用好马战胜和用这匹马战胜的效果相同,剩下的那匹肯定比国王剩下的马都强。
若不用最好的马去战斗,若不能战胜那肯定不用说是费的;若能战胜,那么肯定也能战胜国王剩余的马,保留这匹马和保留最好的马的效果一样。
dp[i][j]=max(dp[i-1][j]+judge(n-i+j,i-1),dp[i-1][j-1]+judge(j-1,i-1));
2)贪心
在学了dp方法之后,又学了贪心的思想。
将田忌和国王的马都从大到小排序,执行以下步骤
//下面说的最快和最慢都是在经过上次操作后剩下的马。
①每次将田忌剩余马中最快的马和国王剩余马忠最快的马比较,若赢就跳到②,输跳到③,平跳到④。直到所有马都比完为止
②田忌用剩余最快的马战胜国王剩余的最快的马,(即使田忌有别的马能战胜国王最快的马,那用别的马和最快的马效果一样,因为剩下的另一匹马还是都大于国王剩下的马)回到①
③田忌用最慢的马顶掉国王最快的马(物尽其用,反正对国王最快的马怎么都要输,那就用最慢的马好了),回到①
④比较田忌最慢的马和国王最慢的马,若田忌的马胜跳到⑤,负跳到⑥,平跳到⑦
⑤用田忌最慢的马战胜国王最慢的马(没有平局的情况下的贪心,也是物尽其用),跳回①
⑥用田忌最慢的马顶掉国王最快的马(反正田忌最慢的马都要输,那就把国王最快的马顶掉好了,也是物尽其用),跳回①
⑦用田忌最慢的马顶掉国王最快的马(这是难点,主要的问题就出在这里。如果每次遇到平的话都对调,那么最终在这些马上的得等最高只有0分;如果顶掉的话,那么田忌最快的马也肯定可以战胜国王最慢的马,那样可以保证最低0分,在出现田忌【1,2,3】和国王【1,2,3】的情况下,最高分数就不是0分了,所以贪心时选择用田忌最慢的马顶掉国王最快的马),跳回①