HDU 1052 田忌赛马


传送门:HDU 1052


Tian Ji -- The Horse Racing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Problem Description
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.

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匹马,判断怎样比赛,使田忌净胜场数最多。

题解
大致思路就是把握所有能赢的分,不能赢的马就为其他马创造赢得机会(消耗更快的马)

1.若田忌最慢的马可以战胜齐王最慢的马,那么就让它战胜那匹慢马。

2.若田忌最慢的马不能战胜齐王最慢的马,那么它更加不能战胜其他的马,那就让它输,而且输给齐王最快马。

3.若田忌最慢的马与齐王最慢的马速度相等。此时,不能简单地认为与它打成平手就是最好情况,相反,打平是下下策,为什么呢?

因为自己除了最慢的马之外的所以马都可以赢齐王最慢的马(因为双方最慢的马速度相等),所以就算自己输一场,其他马也能赢回一场。虽然一胜一输的收益和打平的收益是一样的,但是自己最慢的马输的时候可以拉对方更快的马下水,给己方最快的马创造更大的胜利机会,也就是说己方最快的马很可能因为自己的输再胜利一场。

但是,还有一点需要注意,当自己放水前,如果己方最快的马原本就比对方最快的马快,然后还输给对方最快的马,那么己方最快的马就浪费了,因为本来就可以赢,无须用最慢的马来消耗,不如去帮其他马。

所以,在消耗的时候,一定要保证己方最快马不快于对方最快马。满足此条件后,让己方最慢马与对方最快马去比赛(有可能平局),这样,田忌的马就得到了充分的利用。


AC代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#define N 1010
using namespace std;
int a[N],b[N];
int main()
{
  int n;
  while(~scanf("%d",&n)&&n)
  {
    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);
    sort(b,b+n);
    int afast=n-1,bfast=n-1,aslow=0,bslow=0;
    int win=0,lose=0;
    while(afast>=aslow)
    {
      if(a[aslow]>b[bslow])//如果田最慢的马比王最慢的马快,赢一场
      {
        win++;
        aslow++;
        bslow++;
      }
      else if(a[aslow]<b[bslow])//如果慢,输一场,用最慢的马比王最快的马
      {
        lose++;
        aslow++;
        bfast--;
      }
      else//如果相等
      {
        if(a[afast]>b[bfast])//如果田最快的马比王最快的马快,不需要输,
        {
          win++;
          afast--;
          bfast--;
        }
        else
        {
          if(a[aslow]<b[bfast])//可能抵消及最快的马速度相等的情况平局;
          {
            lose++;
          }
          aslow++;
          bfast--;
        }
      }
    }
    int t;
    t=win-lose;
    printf("%d\n",200*t);
  }
  return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值