[HDOJ1052]Tian Ji -- The Horse Racing

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052

贪心,田忌的策略比较复杂,仔细想一下可过。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 int main()  {
 7     int n;
 8     int th[1000], qh[1000];
 9     int win = 0;
10     while(scanf("%d", &n), n) {
11         int tmax, tmin, qmax, qmin;
12         win = 0;
13         for(int i = 0; i < n; i++) {
14             scanf("%d", &th[i]);
15         }
16         for(int i = 0; i < n; i++) {
17             scanf("%d", &qh[i]);
18         }
19         sort(th, th+n);
20         sort(qh, qh+n);
21         tmax = n - 1;
22         qmax = n - 1;
23         tmin = 0;
24         qmin = 0;
25         while(tmax >= tmin) {
26             if(th[tmax] > qh[qmax]) {
27                 tmax--;
28                 qmax--;
29                 win++;
30             }
31             else {
32                 if(th[tmin] > qh[qmin]) {
33                     tmin++;
34                     qmin++;
35                     win++;
36                 }
37                 else {
38                     if(th[tmin] < qh[qmax]) {
39                         tmin++;
40                         qmax--;
41                         win--;
42                     }
43                     else {
44                         tmin++;
45                         qmax--;
46                     }
47                 }
48             }
49         }
50         printf("%d\n", win*200);
51     }
52     return 0;
53 }

时间: 2024-10-13 16:19:06

[HDOJ1052]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