题意:田忌和齐王都有 n(n <= 1000)匹马,二者进行 n 轮赛马,赢一局+200,平一局得 0,输一局 -200,求田忌最多能赢多少钱。

贪心,先将田忌和齐王的马分别排序,然后最上等与最上等比,最下等与最下等比,如果有能赢的则+200,如果无法满足条件,则用田忌的最下等与齐王的最上等比(故意输或者平局)。

代码如下:

 1 #pragma comment(linker, "/STACK:102400000, 102400000")
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cctype>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<iostream>
 8 #include<sstream>
 9 #include<iterator>
10 #include<algorithm>
11 #include<string>
12 #include<vector>
13 #include<set>
14 #include<map>
15 #include<deque>
16 #include<queue>
17 #include<stack>
18 #include<list>
19 #define fin freopen("in.txt", "r", stdin)
20 #define fout freopen("out.txt", "w", stdout)
21 #define pr(x) cout << #x << " : " << x << "   "
22 #define prln(x) cout << #x << " : " << x << endl
23 #define Min(a, b) a < b ? a : b
24 #define Max(a, b) a < b ? b : a
25 typedef long long ll;
26 typedef unsigned long long llu;
27 const int INT_INF = 0x3f3f3f3f;
28 const int INT_M_INF = 0x7f7f7f7f;
29 const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
30 const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
31 const double pi = acos(-1.0);
32 const double EPS = 1e-8;
33 const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
34 const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
35 const ll MOD = 1e9 + 7;
36 using namespace std;
37
38 #define NDEBUG
39 #include<cassert>
40 const int MAXN = 1000 + 10;
41 const int MAXT = 10000 + 10;
42
43 int n;
44 deque<int> t, k;
45
46 int main(){
47     while(scanf("%d", &n) == 1 && n){
48         int tmp;
49         t.clear();
50         k.clear();
51         for(int i = 0; i < n; ++i)  {scanf("%d", &tmp);  t.push_back(tmp);}
52         for(int i = 0; i < n; ++i)  {scanf("%d", &tmp);  k.push_back(tmp);}
53         sort(t.begin(), t.end());
54         sort(k.begin(), k.end());
55         int ans = 0;
56         while(n--){
57             if(t.front() > k.front()){
58                 t.pop_front();
59                 k.pop_front();
60                 ans += 200;
61             }
62             else if(t.back() > k.back()){
63                 t.pop_back();
64                 k.pop_back();
65                 ans += 200;
66             }
67             else{
68                 if(t.front() != k.back())  ans -= 200;
69                 t.pop_front();
70                 k.pop_back();
71             }
72         }
73         printf("%d\n", ans);
74     }
75     return 0;
76 }

转载于:https://www.cnblogs.com/tyty-TianTengtt/p/6089550.html

POJ 2287 - Tian Ji -- The Horse Racing(贪心)相关推荐

  1. poj hdu Tian Ji -- The Horse Racing 贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=1052 田忌赛马 先把田忌和国王的马排序. 每次取田忌的最快的马与国王最快的马比较,有三种情况. 一,田忌最快的马 ...

  2. Tian Ji -- The Horse Racing(贪心+STL)

    Problem:Tian Ji – The Horse Racing Description: Here is a famous story in Chinese history. "Tha ...

  3. Tian Ji -- The Horse Racing 贪心算法

    Problem Description Here is a famous story in Chinese history. "That was about 2300 years ago. ...

  4. 2018北大暑校acm算法训练课程 Tian Ji -- The Horse Racing 贪心

    总时间限制: 5000ms 内存限制: 65536kB 描述 Here is a famous story in Chinese history. That was about 2300 years ...

  5. UVa1344 - Tian Ji -- The Horse Racing(贪心)

    问题:田忌和国王都有n匹马,比赛n场.赢一场得200个银币,平局不得也不失,输失去200个银币.部田忌最多可以赢多少个银币? 思路:贪心算法.将田忌和国王的n匹马分别按马速从小到大排列.贪心策略如下: ...

  6. 贪心法田忌赛马问题Java代码,hdoj 1052 Tian Ji - The Horse Racing【田忌赛马】 【贪心】...

    hdoj 1052 Tian Ji -- The Horse Racing[田忌赛马] [贪心] 思路:先按从小到大排序, 然后从最快的开始比(假设i, j 是最慢的一端, flag1, flag2是 ...

  7. Tian Ji -- The Horse Racing(田忌赛马)/贪心算法

    Tian Ji – The Horse Racing(田忌赛马) 田忌赛马的故事 (可以直接看题) Here is a famous story in Chinese history. "T ...

  8. 贪心算法 003:Tian Ji -- The Horse Racing

    003:Tian Ji – The Horse Racing 总时间限制: 5000ms 内存限制: 65536kB 描述 Here is a famous story in Chinese hist ...

  9. hdu-1052 Tian Ji -- The Horse Racing

    HOT!!! 欢迎参加"金山西山居-2013创意游戏程序挑战赛"! Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Ja ...

最新文章

  1. LOJ 数列分块入门6
  2. h5页面笔按下默认是拖动_屡屡刷屏的长页面H5原来是这样诞生的
  3. Github上LeakCanary编译报错CreateProcess error=2的解决方法
  4. Ios KVC KVO
  5. AcWing 788 逆序对的数量-归并排序
  6. volatile、static
  7. java 自动启动监听_Spring Boot 启动事件和监听器,太强大了!
  8. rand.nextint()
  9. redis 安装并设置为开机启动服务
  10. mysql 数据透视_sql怎么做数据透视表
  11. VS2015配置Opencv
  12. linux段错误吐核怎么办,CentOS命令
  13. java使用xsd校验xml样例
  14. 【转】NB的specify
  15. K-Means原理及代码实现
  16. Ubuntu16.04+Python3.6+深度学习环境+opencv+x264+ffmpeg基础镜像制作实战
  17. 计算机动画---计算机动画的光栅方法
  18. HttpClient 通过资源URL下载资源
  19. MPMLink 中 通过 零件号+MBOM版本,查询正确的EBOM版本(相当于对等部件功能)
  20. Cloning into ‘vue-element-admin‘... fatal: unable to access ‘https://github.com/PanJiaChen/vue-eleme

热门文章

  1. vc中如何将一个工程的资源复制到另外一个工程?
  2. freemarker结合springMVC配置
  3. 100个最古老互联网域名 最久只有23年(附名单)
  4. 我的天!我靠这个拥有了自己第一架无人机!
  5. CVPR自动驾驶运动预测挑战赛:轻舟智航夺冠方案
  6. 深度学习人体姿态估计:2014-2020全面调研
  7. OpenMMLab的新篇章
  8. 我爱计算机视觉精华文章分类汇总(2018年12月13日)
  9. Intel刚刚收购的Vertex.AI,到底有什么黑科技?
  10. jsp mysql环境_MySQL在JSP环境下的操作应用