LA 3266 (贪心) Tian Ji -- The Horse Racing

题意:

田忌和齐王各有n匹马,如果马的速度比齐王的快就赢200,慢则输200,相等不赔不赚。

已知两人每匹马的速度(为整数)和齐王所排出的马的顺序,问田忌该如何应对才能使收益最大。

分析:

本以为是一道很简单的贪心,上来就贪,结果什么都没贪出来。

看了题解才发现贪心是比较复杂的。

这里贴上poj某牛的神分析。

贪心策略:
1,如果田忌的最快马快于齐王的最快马,则两者比。
(因为若是田忌的别的马很可能就赢不了了,所以两者比)
2,如果田忌的最快马慢于齐王的最快马,则用田忌的最慢马和齐王的最快马比。
(由于所有的马都赢不了齐王的最快马,所以用损失最小的,拿最慢的和他比)
3,若相等,则比较田忌的最慢马和齐王的最慢马
3.1,若田忌最慢马快于齐王最慢马,两者比。
(田忌的最慢马既然能赢一个就赢呗,而且齐王的最慢马肯定也得有个和他比,所以选最小的比他快得。)
3.2,其他,则拿田忌的最慢马和齐王的最快马比。
(反正所有的马都比田忌的最慢马快了,所以这匹马必输,选贡献最大的,干掉齐王的最快马)

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4
 5 const int maxn = 1000 + 10;
 6 int tian[maxn], king[maxn];
 7
 8 int main(void)
 9 {
10     //freopen("3266in.txt", "r", stdin);
11     int n;
12     while(scanf("%d", &n) == 1 && n)
13     {
14         for(int i = 0; i < n; ++i) scanf("%d", &tian[i]);
15         for(int i = 0; i < n; ++i) scanf("%d", &king[i]);
16         sort(tian, tian + n);
17         sort(king, king + n);
18
19         int thead = 0, khead = 0;
20         int ttail = n-1, ktail = n-1;
21         int win = 0;
22
23         while(n--)
24         {
25             if(tian[ttail] > king[ktail]) //Èç¹ûÌï¼ÉµÄ×î¿ìÂí¿ìÓÚÆëÍõµÄ×î¿ìÂí£¬ÔòÁ½Õ߱ȡ£
26             {
27                 ttail--;
28                 ktail--;
29                 win++;
30             }
31             else if(tian[ttail] < king[ktail]) //Èç¹ûÌï¼ÉµÄ×î¿ìÂíÂýÓÚÆëÍõµÄ×î¿ìÂí£¬ÔòÓÃÌï¼ÉµÄ×îÂýÂíºÍÆëÍõµÄ×î¿ìÂí±È¡£
32             {
33                 thead++;
34                 ktail--;
35                 win--;
36             }
37             else //ÈôÏàµÈ£¬Ôò±È½ÏÌï¼ÉµÄ×îÂýÂíºÍÆëÍõµÄ×îÂýÂí
38             {
39                 if(tian[thead] > king[khead]) //ÈôÌï¼É×îÂýÂí¿ìÓÚÆëÍõ×îÂýÂí£¬Á½Õ߱ȡ£
40                 {
41                     thead++;
42                     khead++;
43                     win++;
44                 }
45                 else //ÆäËû£¬ÔòÄÃÌï¼ÉµÄ×îÂýÂíºÍÆëÍõµÄ×î¿ìÂí±È¡£
46                 {
47                     if(tian[thead] < king[ktail]) win--;
48                     thead++;
49                     ktail--;
50                 }
51             }
52         }
53
54         printf("%d\n", win * 200);
55     }
56
57     return 0;
58 }

代码君

时间: 2024-08-10 02:08:37

LA 3266 (贪心) Tian Ji -- The Horse Racing的相关文章

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 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的要求. 我们不妨用贪心思想来分析一下问题.因为田忌掌握有比赛的“主动权”,他总是根据齐王所出的马来分配自己的马,所以这里不妨认为齐王的出马顺序是按马的速度从高到低出的.由这样的假设,我们归纳出如下贪心

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

HDU1052 Tian Ji -- The Horse Racing 贪心

Tian Ji -- The Horse Racing Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1052 Description Here is a famous story in Chinese history. "That was about 2300 years ago. General Tian Ji was a

杭电 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 fa

HDU 1052.Tian Ji -- The Horse Racing【很好的贪心】【8月27】

Tian Ji -- The Horse Racing 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

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 可恶的贪心-------也算是经典贪心题吧,对于一般人来说,不看题解,应该很难做出来吧

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