hdu1052

本文解析了田忌赛马这一经典问题,并通过C和C++两种语言实现算法对比,探讨了不同语言下代码实现的差异及原因。文章详细介绍了问题背景、输入输出要求、示例、解题思路及代码实现。

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

                          Tian Ji -- The Horse Racing

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3900 Accepted Submission(s): 935
 
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
 

涉及知识点:贪心算法,排序,动态规划;

我用c++写的代码会出错,改用c语言写后却正确ac,不明白为什么求大神指点一二!!

我写的c++代码:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 void sort(int a[],int t)      //从高到低排序
 5 {
 6     for(int i=0;i<t;i++)
 7         for(int j=i+1;j<t;j++)
 8             if(a[i]<a[j])
 9                 swap(a[i],a[j]);
10 }
11 
12 int main()
13 {
14     int ti[50],king[50];
15     int n,i,tfast,kfast,tslow,kslow,sum;
16     while(cin>>n&&(n!=0))
17     {
18         for(i=0;i<n;i++)
19             cin>>ti[i];
20             sort(ti,n);
21         for(i=0;i<n;i++)
22             cin>>king[i];    
23             sort(king,n);            
24         tfast=kfast=0;
25         tslow=kfast=n-1;
26         sum=0;
27         for(i=0;i<n;i++)
28         {
29             if(ti[tfast]==king[kfast])
30             {
31                 if(ti[tslow]>king[kslow]){sum++;tslow--;kslow--;}
32                 else if(ti[tslow]<king[kslow]){sum--;tslow--;kfast++;}
33                 else if(ti[tslow]==king[kslow])
34                 {
35                     if(ti[tslow]<king[kfast]){sum--;tslow--;kfast++;}
36                     else if(ti[tslow]==king[kfast]){    break;}                  //error?
37                 }
38             }
39             else if(ti[tfast]<king[kfast]){sum--;tslow--;kfast++;}
40             else if(ti[tfast]>king[kfast]){sum++;tfast++;kfast++;}
41                 
42         }
43         cout<<sum*200<<endl;
44         
45     }
46     
47     return 0;
48 }

能正确提交的c代码:

 1 #include<stdio.h>
 2 void sort(int a[],int t)      //从高到低排序
 3 {
 4     int temp,i,j;
 5     for(i=0;i<t;i++)
 6         for(j=i+1;j<t;j++)
 7             if(a[i]<a[j])
 8             {
 9                 temp=a[i];
10                 a[i]=a[j];
11                 a[j]=temp;
12             }    
13 }
14 int main()
15 { 
16     int n,i,sub=0,sum=0;
17     int tfast,tslow,wfast,wslow;  //分别代表田的最快马,最慢马,王的最快马,最慢马
18     int t[1000],w[1000];
19     while(scanf("%d",&n)&&(n!=0))
20     {   
21         for(i=0;i<n;i++)
22             scanf("%d",&t[i]);
23         for(i=0;i<n;i++)
24             scanf("%d",&w[i]);
25         sort(t,n);
26         sort(w,n);
27 
28         tfast=0;tslow=n-1;
29         wfast=0;wslow=n-1;    //最快最慢马的初始化
30    
31         for(i=0;i<n;i++)         //分三种情况
32         {
33             if(t[tfast]==w[wfast])    //情况1
34             {
35                 if(t[tslow]>w[wslow]){sub++;tslow--;wslow--;}
36                 else if(t[tslow]<w[wslow]){sub--;tslow--;wfast++;}
37                 else if(t[tslow]==w[wslow])
38                 {
39                     if(t[tslow]<w[wfast]){sub--;tslow--;wfast++;}
40                     else if(t[tslow]==w[wfast]){break;}
41                 }
42             }
43             else if(t[tfast]>w[wfast])     //情况2
44             {sub++;tfast++;wfast++;}
45             else    {sub--;tslow--;wfast++;} //情况3
46         }
47         sum=sub*200;
48         printf("%d\n",sum);
49         sub=0;sum=0;
50     }
51   return 0;
52 }

 

转载于:https://www.cnblogs.com/mm-happy/p/3800950.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值