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 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.

Input
The input consists of up to 50 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. The input ends with a line that has a single 0 after the last test case.

Output
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.

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

AC:
#include <bits/stdc++.h>
using namespace std;
#define MXN 1010
int n, t[MXN], k[MXN];
int main(){
while(scanf("%d", &n)){
if(n == 0) break;
for(int i = 1; i <= n; i++) scanf("%d", t+i);
for(int i = 1; i <= n; i++) scanf("%d", k+i);
sort(t+1, t+n+1, [](int x, int y){ return x > y; });
sort(k+1, k+n+1, [](int x, int y){ return x > y; });
int th = 1, tt = n, kh = 1, kt = n;
int w = 0, d = 0, l = 0;
while(w + d + l < n){
if(t[tt] < k[kt]) kh++, tt–, l++; // 若田的慢马必输时
else if(t[th] > k[kh]) kh++, th++, w++; // 若田的快马必赢时
else if(t[th] < k[kh]) kh++, tt–, l++; // 若王的快马必赢时
else if(t[tt] > k[kt]) tt–, kt–, w++; // 若王的慢马必输时
else{ // 双方都无必输或必赢的马时
if(k[kh] > t[tt]) l++;
else if(k[kh] < t[tt]) w++;
else d++;
kh++, tt–;
}
}
printf("%d\n", (w-l)*200);
}
return 0;
}

TKO 2-2需要考虑周全的贪心问题--田忌赛马相关推荐

  1. 55天 - 贪心算法 - 田忌赛马问题 openjudge百炼 2287

    //模板#include <iostream> #include <cstdio> #include <string> #include <algorithm ...

  2. 贪心算法 田忌赛马问题

    贪心算法 田忌赛马问题 这个题目贪心的本质在于:*田忌只在有把握赢的情况下拿出快马和王拼,否则用最慢的马比掉王的快马最大程度削弱王的战斗力 贪心策略: 1,如果田忌的最快马快于齐王的最快马,则两者比. ...

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

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

  4. 典型的贪心算法~ (田忌赛马 )

    1. 田忌赛马  典型的贪心算法~~自己木有考虑到贪心的第二步导致wa了好多次 算法分析 Problem Description: 给出2N组数据,分别表示田忌和齐威王的N匹马的速度,没进行一场比赛( ...

  5. 田忌赛马贪心算法_贪心算法--田忌赛马问题

    题目描述: 你一定听过田忌赛马的故事吧? 如果3匹马变成1000匹,齐王仍然让他的马按从优到劣的顺序出赛,田忌可以按任意顺序选择他的赛马出赛.赢一局,田忌可以得到200两银子,输一局,田忌就要输掉20 ...

  6. 【贪心】田忌赛马题解

    题目描述 田忌准备和齐王赛马,各自拿出的比赛马匹数是n个,胜负由每匹马的速度决定,田忌可 以自由选择自己的马和齐王的比赛,田忌赢一次赏金加50,输一次赏金赔50,田忌赚的 钱最低为0,求田忌最多能赚多 ...

  7. 疯子的算法总结(四)贪心算法

    一.贪心算法 解决最优化问题的算法一般包含一系列的步骤,每一步都有若干的选择.对于很多最优化问题,只需要采用简单的贪心算法就可以解决,而不需要采用动态规划方法.贪心算法使所做的局部选择看起来都是当前最 ...

  8. bzoj 1034: [ZJOI2008]泡泡堂BNB(贪心)

    1034: [ZJOI2008]泡泡堂BNB Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 3341  Solved: 1708 [Submit][ ...

  9. Shell 开发在运维中的经验总结

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 无论是系统运维,还是应用运维,均可分为"纯手工" ...

最新文章

  1. 中国航发9名劳模工匠变身“高级制造工程师”
  2. 用Maven构建Hadoop项目
  3. Qtopia-2.2.0启动脚本
  4. python几种设计模式_Python七大原则,24种设计模式
  5. k3系统 中间层服务器,金蝶k3中间层服务器如何设置
  6. ORBSLAM3 的改进
  7. python做生物信息学分析_Python从零开始第五章生物信息学①提取差异基因
  8. jensen不等式(jensen不等式对于凹函数)
  9. imx6q的启动方式
  10. 用手机打开word图表位置很乱_9个工作中经常用到的Word技巧,能大大提升你的工作效率...
  11. 解决File Cache Conflict
  12. 黑客电影《我是谁:没有绝对安全的系统》正片(含下载)
  13. dss中文含义_DSS是什么意思?
  14. 一个不错的shell 脚本教程
  15. Altium Designer 14.1.5 Build 30772
  16. Vue学习25_Vue的异步操作Promise
  17. error: (-5:Bad argument) image is empty or has incorrect depth (!=CV_8U) in function ‘cv::SIFT_Impl:
  18. 艾美捷CpG-A DNA,人/小鼠的功能和应用
  19. steam+linux+吃鸡游戏,steam十款免费吃鸡大逃杀游戏推荐
  20. ARM——LED灯实验

热门文章

  1. 纯JS实现兼容IE8的多文件下载
  2. python中年大叔学编程_中年大叔学编程-Python环境安装
  3. 全球模具经典案例|SPC软件在开模流程中的应用
  4. 【C语言期末/实践/大作业】成绩管理系统日程表管理系统
  5. python中ddt的安装使用
  6. creator 跳跃弧线_(转)CocosCreator零基础制作游戏《极限跳跃》一、游戏分析
  7. C语言中的stdlib头文件解析
  8. 完整获取图片根据图片链接地址
  9. 原型设计大师:Axure RP网站与APP设计从入门到精通(全彩) 中文pdf扫描版
  10. SS2022-Z变换-性质-什么是ZT初值和终值定理?