HDU 1052 - Tian Ji -- The Horse Racing

本文解析了一个经典的田忌赛马问题,通过两种不同的贪心算法实现,旨在找到最佳匹配策略,使田忌在比赛中获得最大收益。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

题意分析:
  田忌赛马,赢了拿200,输了赔200,问你能赚多少钱。
解题思路:
 贪心,以下有两个代码:
  第一个代码:
  排序完从快到慢第一次比较,然后将田忌的慢马一匹一匹拉上来和齐王快马比(请见循环),找出赢得最多的就是了。(那个乱入的快排请随意)
  田忌速度:
     开始:5 4 3 2 1
   开始循环:1 5 4 3 2
        1 2 5 4 3
        1 2 3 5 4
        1 2 3 4 5
  如此,选赢的最多的即可。 
  第二个代码:
   排序完,本着把要输的慢马和齐王的快马比的原则。
  如果慢马比齐王快,那赢一场;
  如果快马比齐王快,那赢一场;
  如果慢马和齐王快马一样快,那平局。
  剩下的都是输。
 
 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     void qsort(int c[],int,int);
 6     int n,ans;
 7     int a[1500];
 8     int b[1500];
 9     while(scanf("%d",&n),n)
10     {
11         for(int i=1;i<=n;i++)    scanf("%d",&a[i]);
12         for(int i=1;i<=n;i++)    scanf("%d",&b[i]);
13         qsort(a,1,n);
14         qsort(b,1,n);
15         int ans=0;
16         for(int i=1;i<=n;i++){
17             if(a[i]>b[i]) ans+=200;
18             else if(a[i]<b[i]) ans-=200;
19         }
20         for(int i=1;i<=n-1;i++){
21             int t,max=0;
22             t=a[n];//把慢马放前面,然后依次往下放 
23             for(int j=n;j>i;j--)    a[j]=a[j-1];
24             a[i]=t;
25             for(int j=1;j<=n;j++){
26                 if(a[j]>b[j]) max+=200;
27                 else if(a[j]<b[j]) max-=200;
28             }
29             if(max>ans){
30                 ans=max;
31             }
32         }
33         printf("%d\n",ans);
34     }
35  } 
36  void qsort(int c[],int left,int right)//快排 
37  {
38      if(left<right){
39      int i=left;
40      int j=right;
41      int x=c[i];
42      while(i<j){
43          while(i<j && c[j]<x)  j--;
44          if(i<j){
45              c[i]=c[j];
46              i++;
47          }
48          while(i<j && c[i]>x)  i++;
49         if(i<j){
50             c[j]=c[i];
51             j--;
52         }
53      }
54      c[i]=x;
55      qsort(c,left,i-1);
56      qsort(c,i+1,right);
57     }
58  }
 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 int main(){
 5     int n;
 6     while(scanf("%d",&n),n){
 7         int t[1005],q[1005];
 8         for(int i=0;i<n;i++) scanf("%d",&t[i]);
 9         for(int i=0;i<n;i++) scanf("%d",&q[i]);
10         sort(t,t+n); sort(q,q+n);
11         int mit=0,mat=n-1;//tian_min、max 
12         int miq=0,maq=n-1;//qi_min、max 
13         int ans=0;
14         while(mit<=mat){
15             if(t[mit]>q[miq]) {ans++;mit++;miq++;}
16             else if(t[mat]>q[maq]) {ans++;mat--;maq--;}
17             else if(t[mit]==q[maq]){mit++;maq--;}
18             else {ans--;mit++;maq--;}
19         } printf("%d\n",ans*200);
20     } return 0;
21 }

 

转载于:https://www.cnblogs.com/nicetomeetu/p/5158600.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值