HDOJ-1052 田忌赛马

田忌赛马

时间限制: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 }
时间: 2024-10-29 19:12:23

HDOJ-1052 田忌赛马的相关文章

hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】

思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較,二等于在推断田的最慢的是不是比king的最快的慢,三小于则与king的最快的比較: Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe

hdu 1052(田忌赛马 贪心算法,sort排序)

Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18155    Accepted Submission(s): 5281 Problem Description Here is a famous story in Chinese history. "That was about

hdu 1052 田忌赛马

贪心,排序从大到小.. 先比大的.跑只是就拿最小的来送死.. , 假设是平局就比后面的... 若后面也是平局就拿去跟前面的去跑. .. #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; int s[1005],w[1005]; int main() { int n,i; while(scanf("%d&qu

贪心专题

贪心: 原则是根据固定的一个或几个属性进行抉择.达到缩小规模的目的. split to steps and shrink scope 有点双端/DAG图的意思. reference 最近做的几个贪心题目 hdu 1052(田忌赛马) 根据马的速度排序,然后两边最小值,最大值的判断.这样做是因为两端的选择是唯一的,TJ最弱的马/最强的如果可以战胜那就是最好的抉择,不然就让其去当炮灰.(减少规模) #include<set> #include<cmath> #include<cs

C语言贪心(2)___田忌赛马(Hdu 1052)

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 t

HDU 1052 Tian Ji -- The Horse Racing (田忌赛马)

田忌和齐王各有N匹马,判断怎样比赛,使田忌净胜场数最多. 之前无意看到强哥写的题解(很早就做了~~囧)感觉很有意思,但是当时忘了去A 了,现在回想起来此题很是经典了,意犹未尽的感觉,直接复制题解了,思路写的很清楚了, 基本就是看着思路敲的 [解题思路]不管怎么比赛 http://www.midifan.com/moduleuser-index-400171.htmhttp://www.midifan.com/moduleuser-index.htmhttp://www.midifan.com/m

【贪心专题】HDU 1052 Tian Ji -- The Horse Racing (田忌赛马)

链接:click here~~ 题意: 田忌和齐王各有N匹马,判断怎样比赛,使田忌净胜场数最多. 之前无意看到强哥写的题解(很早就做了~~囧)感觉很有意思,但是当时忘了去A 了,现在回想起来此题很是经典了,意犹未尽的感觉,直接复制题解了,思路写的很清楚了, 基本就是看着思路敲的 [解题思路]不管怎么比赛,都要让田忌的马发挥最大价值.当然,马的第一要务是用来赢得比赛,而且要最大效益的赢,也就是要赢对方仅次于自己的马. 当他不能完成这个任务的时候就要去输,并拉对方最快的马下水,给自己后面的队友创造更

HDU 1052 Tian Ji -- The Horse Racing(贪心)(2004 Asia Regional Shanghai)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 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

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>