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

HDOJ-1052 田忌赛马

时间:2017-01-03 23:59:28      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:include   bip   money   perfect   scan   ini   raw   owa   which   

田忌赛马

时间限制:3000 ms | 内存限制:65535 KB 
难度:3 
描述:

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. 
输入 
The input consists of many 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. 
输出 
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.

样例输入


92 83 71 
95 87 74 

20 20 
20 20 

20 19 
22 18

 

样例输出

200 

0

 

贪心策略:

如果 田忌最弱>大王最弱: 
田忌最弱PK大王最弱

如果 田忌最弱<大王最弱: 
田忌最弱PK大王最强

如果 田忌最弱=大王最弱{ 
如果田忌最强>大王最强 田忌最强PK大王最强 
否则:田忌最弱PK大王最强 
}

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int compare(const void * p1, const void * p2) {
 4     return *(int *)p2 - *(int *)p1;
 5 }
 6 int main(void)
 7 {
 8     int n;
 9     while (~scanf("%d", &n) && n) {
10         int tian[1000];
11         int king[1000];
12         for (int i = 0; i < n; i++) {
13             scanf("%d", &tian[i]);
14         }
15         qsort(tian, n, sizeof(int), compare);
16         for (int i = 0; i < n; i++) {
17             scanf("%d", &king[i]);
18         }
19         qsort(king, n, sizeof(int), compare);
20         if (tian[0] < king[n - 1]) {
21             printf("%d\n", -(200 * n));
22             continue;
23         }
24         else if (tian[n - 1] > king[0]) {
25             printf("%d\n", 200 * n);
26             continue;
27         }
28         int min_t = n - 1, min_k = n - 1;
29         int max_t = 0, max_k = 0;
30         int win = 0;
31         while (n--) {
32             if (tian[min_t] > king[min_k]) {
33                 //田弱vs王弱
34                 win++;
35                 min_t--;
36                 min_k--;
37                 continue;
38             }
39             else if (tian[min_t] < king[min_k]) {
40                 //田弱vs王强
41                 if (tian[min_t] != king[max_k])
42                     win--;
43                 min_t--;
44                 max_k++;
45                 continue;
46             }
47             else {
48                 if (tian[max_t] > king[max_k]) {
49                     //田强vs王强
50                     win++;
51                     max_t++;
52                     max_k++;
53                     continue;
54                 }
55                 else {
56                     //田弱vs王强
57                     if (tian[min_t] != king[max_k])
58                         win--;
59                     min_t--;
60                     max_k++;
61                     continue;
62                 }
63             }
64         }
65         printf("%d\n", win * 200);
66     }
67 }

 

HDOJ-1052 田忌赛马

标签:include   bip   money   perfect   scan   ini   raw   owa   which   

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!