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. Problem A: 编写函数:三个数的最大最小值 (Append Code)
  2. poxtfix+dovecot+saslauthd+courier-authlib +mysql + extmail 完整虚拟邮箱系统部署
  3. java程序运行过程数据丢失怎么办_java运行过程中OutOfMemoryError是什么原因?怎么解决...
  4. 面试硬核干货:纯CSS实现垂直居中,快来收藏吧
  5. 软件测试HW3 主路径覆盖测试
  6. 中医移动医疗_人工智能为中医赋能 上海祉云医疗将再次亮相2021健博会北京展...
  7. win7蓝牙驱动的使用方法
  8. npm connect ETIMEDOUT
  9. linux zip,tar压缩文件夹 忽略 .git 文件夾
  10. android网速代码,Android获取网速和下载速度
  11. 星起航:亚马逊全球开店品牌负责人唐浩表示“中国品牌出海的黄金时代已经到来”
  12. linux中如何开启vnc服务端口,Linux下vnc配置及启动
  13. 国外最好用的WordPress主机推荐
  14. 设计师接私活的一些忠告
  15. 最大奇约数(c++实现)
  16. vulnhub bulldog
  17. 单向可控硅和双向可控硅的详细介绍(含引脚的分辨)
  18. currentTimeMillis()的解释
  19. phpstorm破解后,运行一段时间后突然有提示没有破解.
  20. GOF设计模式之组合设计模式(结构型模式) ✨ 每日积累

热门文章

  1. 用晨曦记账本,设置多功能打印账目
  2. Logback新版本报no applicable action for [Encoding]问题
  3. springboot+jsp汽车在线销售系统
  4. 安装XenServer
  5. 1python软件的下载官网是_学Python软件下载-学Python软件官方版下载v1.0-upan
  6. creator 跳跃弧线_CocosCreator零基础制作游戏《极限跳跃》
  7. window对象,对话框
  8. 百度搜索结果的URL参数 相关提示位置(rsp)
  9. 深度学习day05 用Pytorch实现线性回归
  10. 等保二级和等保三级有哪些区别