码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1052 Tian Ji -- The Horse Racing (贪心)

时间:2014-08-13 00:41:54      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

Tian Ji -- The Horse Racing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18154    Accepted Submission(s): 5280


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?"

bubuko.com,布布扣

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
 

 

Source
 

 

Recommend
JGShining
 
很经典的一道贪心题
题目的意思就是说,田忌和国王赛马,每个人有n匹马,每一匹马都有相应的速度,问田忌能赢得最多的钱是多少?
 
贪心策略如下:
1.当田忌最慢的马比齐王最慢的马快时,用最慢的马赢一场
2.当田忌最慢的马比齐王最慢的马慢时,就用田忌最慢的马去和齐王最快的马比,输一场
3.此时剩下两种最慢的马速度相同的情况,再考虑快马:
(1)当田忌最快的马比齐王最快的马快时,用最快的马赢一场
(2)当田忌最快的马比齐王最快的马慢时,就用田忌最慢的马去和齐王最快的马比,输一场
最后就剩下最快和最慢的马都相同的情况了:
如果田忌最慢的马比齐王最快的马慢时,输一场
否则是平局
 
bubuko.com,布布扣
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<stdlib.h>
 4 #include<algorithm>
 5 using namespace std;
 6 const int MAXN=1000+10;
 7 int a[MAXN],b[MAXN];
 8 int main()
 9 {
10     //freopen("in.txt","r",stdin);
11     int n;
12     while(scanf("%d",&n),n)
13     {
14         for(int i=0;i<n;i++)
15             scanf("%d",&a[i]);
16         for(int i=0;i<n;i++)
17             scanf("%d",&b[i]);
18         sort(a,a+n);
19         sort(b,b+n);
20         int ans=0;
21         int ap=0,aq=n-1;
22         int bp=0,bq=n-1;
23         while(ap<=aq)
24         {
25             if(a[ap]>b[bp])//田最慢的马快时
26             {
27                 ap++;
28                 bp++;
29                 ans+=200;
30             }
31             else if(a[ap]<b[bp])//田最慢的马慢时
32             {
33                 ap++;
34                 bq--;
35                 ans-=200;
36             }
37             else if(a[aq]>b[bq])//田最快的马快时
38             {
39                 aq--;
40                 bq--;
41                 ans+=200;
42             }
43             else if(a[aq]<b[bq])//田最快的马慢时
44             {
45                ap++;
46                bq--;
47                ans-=200;
48             }
49             //剩下就是最快的马和最慢的马都相等的情况
50             else if(a[ap]<b[bq])//慢马的速度比快马的速度小时
51             {
52                 ap++;
53                 bq--;
54                 ans-=200;
55             }
56             else//这是都相等的情况
57             {
58                 ap++;
59                 bq--;
60             }//这里不可能出现田的慢马比齐王的快马还要快的情况,因为这样的话,田的快马就比齐王的快马速度要了
61         }
62         printf("%d\n",ans);
63     }
64     return 0;
65 }
View Code

 

 

 

HDU 1052 Tian Ji -- The Horse Racing (贪心),布布扣,bubuko.com

HDU 1052 Tian Ji -- The Horse Racing (贪心)

标签:des   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/clliff/p/3908702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!