Tian Ji -- The Horse Racing

题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=16442
题意:

田忌赛马问题。多组案例,以输入n值为0时结束。田忌与国王各有n匹马,进行n场比赛,规定每匹马只能上场一次,每场比赛获胜则获得200金币,否则扣除200金币。给定双方马速,求田忌最多获取多少金币。
案例:

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金币,否则用田忌最慢马匹消耗国王最快马匹。注意!田忌最慢马匹不一定输给国王最快马匹,也可能平局。

源代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 using namespace std;
 5 int n,ant,i,j;
 6 int a[1005],b[1005];
 7 void cmp()
 8 {
 9
10     int x1=0,x2=n-1,y1=0,y2=n-1;
11     while(x1<=x2)
12     {
13         if(a[x1]>b[y1])//双方最慢的马进行比较,田忌胜
14         {
15             ant+=200;
16             x1++;
17             y1++;
18         }
19         else if(a[x1]<b[y1])//田忌输,用最慢的马与国王最快的马竞赛
20         {
21             ant-=200;
22             x1++;
23             y2--;
24         }
25         else//马速相等
26         {
27             if(a[x2]>b[y2])//双方最快的马进行竞赛,田忌胜
28             {
29                 ant+=200;
30                 x2--;
31                 y2--;
32             }
33             else
34             {
35                 if(a[x1]<b[y2])//用田最慢的马输给国王最快的马
36                    ant-=200;
37                 x1++;
38                 y2--;
39             }
40         }
41     }
42 }
43 int main()
44 {
45     while(scanf("%d",&n)&&n)
46     {
47         for(i=0;i<n;i++)
48             scanf("%d",&a[i]);//田忌方马速
49         for(i=0;i<n;i++)
50             scanf("%d",&b[i]);//国王方马速
51         sort(a,a+n);//排序
52         sort(b,b+n);
53         ant=0;//马赛前田忌拥有金币
54         cmp();
55         printf("%d\n",ant);
56     }
57     return 0;
58 }
时间: 2024-10-11 17:47:47

Tian Ji -- The Horse Racing的相关文章

poj 2287 Tian Ji -- The Horse Racing

poj 2287 Tian Ji -- The Horse Racing 题意: 田忌赛马 如果3匹马变成1000匹,齐王仍然让他的马按从优到劣的顺序出赛,田忌可以按任意顺序选择他的赛马出赛.赢一局,田忌可以得到200两银子,输一局,田忌就要输掉200两银子,平局的话不输不赢. 请问田忌最多能赢多少银子? 思路: 贪心: 如果当前最好的马可以胜齐王最好的马,那么让这两匹马比一场: 如果当前最差的马能胜齐王最差的马,那么让这两匹马比一场: 如果上面两个条件都不满足,那么让当前最差的马和齐王最好的马

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 Tian Ji -- The Horse Racing【贪心在动态规划中的运用】

算法分析: 这个问题很显然可以转化成一个二分图最佳匹配的问题.把田忌的马放左边,把齐王的马放右边.田忌的马A和齐王的B之间,如果田忌的马胜,则连一条权为200的边:如果平局,则连一条权为0的边:如果输,则连一条权为-200的边. 然而我们知道,二分图的最佳匹配算法的复杂度很高,无法满足N=2000的要求. 我们不妨用贪心思想来分析一下问题.因为田忌掌握有比赛的“主动权”,他总是根据齐王所出的马来分配自己的马,所以这里不妨认为齐王的出马顺序是按马的速度从高到低出的.由这样的假设,我们归纳出如下贪心

hdu1052 Tian Ji -- The Horse Racing

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17346    Accepted Submiss

(hdu step 1.3.3)Tian Ji -- The Horse Racing(田忌赛马)

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

hdu1052——Tian Ji -- The Horse Racing

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

Tian Ji -- The Horse Racing(杭电1052)(贪心)

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

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

Tian Ji -- The Horse Racing Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 56   Accepted Submission(s) : 25 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Here is 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

杭州电 1052 Tian Ji -- The Horse Racing(贪婪)

http://acm.hdu.edu.cn/showproblem.php? pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18058    Accepted Submission(s): 5239 Problem Description Here is a f