田忌赛马

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述
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.

输入
The input consists of many 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.
输出
For each input case, output a line containing a single number, which is the maximum money Tian Ji will get, in silver dollars.
样例输入
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
样例输出
200
0
0
来源

hdu

题意就跟题目一样,田忌赛马,赢一局得200,问最后田忌最多能得多少钱。总的思路就是一个贪心,尽量用慢的马来消耗国王快的马,

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int tj[1010],king[1010];
int n;
int main()
{int t;while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++)scanf("%d",&tj[i]);for(int i=0;i<n;i++)scanf("%d",&king[i]);sort(tj,tj+n);sort(king,king+n);int tjwin=0,kingwin=0;int tjslow=0,kingslow=0;int tjfast=n-1,kingfast=n-1;for(int i=0;i<n;i++){if(tj[tjslow]>king[kingslow]){tjwin++;tjslow++,kingslow++;}else if(tj[tjslow]<king[kingslow]){kingwin++;tjslow++,kingfast--;}else{if(tj[tjfast]>king[kingfast]){tjwin++;tjfast--,kingfast--;}else if(tj[tjfast]<king[kingfast]){kingwin++;tjslow++,kingfast--;}else{if(tj[tjslow]<king[kingfast]){kingwin++;tjslow++,kingfast--;}}}}printf("%d\n",200*(tjwin-kingwin));}return 0;
}

转载于:https://www.cnblogs.com/playboy307/p/5273460.html

nyoj--364--田忌赛马(贪心)相关推荐

  1. NYOJ 364 田忌赛马

    田忌赛马 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 Here is a famous story in Chinese history. "That ...

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

    关于输入 输入包含多组测试数据. 每组测试数据的第一行是一个整数n(1<=n<=1000),表示田忌和齐王都拥有n匹马.接下来一行是n个整数,表示田忌的马的速度,下一行也是n个整数,表示齐 ...

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

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

  4. 田忌赛马贪心算法_田忌赛马 贪心算法

    算法实验课回顾 田忌赛马 问题描述: 你一定听说过田忌赛马的故事吧?如果3匹马变成n匹(n<=100),齐王仍然让他的马按照优到劣的顺序初赛,田忌可以按任意顺序选择他的赛马出赛.赢一局,田忌可以 ...

  5. 田忌赛马-贪心算法实现

    田忌赛马c语言代码实现 1,问题描述 你一定听说过田忌赛马的故事吧?如果3匹马变成n匹(n<=100),齐王仍然让他的马按照优到劣的顺序初赛,田忌可以按任意顺序选择他的赛马出赛.赢一局,田忌可以 ...

  6. 田忌赛马 贪心 注意细节

    http://poj.org/problem?id=2287 田忌赛马 题意:给田忌 和 国王 各自n匹马 比赛n轮 每匹马只能比一场  赢的一方得200 平得0 田忌最多赢多少 思路 贪心:局部最优 ...

  7. POJ2287 田忌赛马---贪心算法

    田忌赛马 题目详见http://poj.org/problem?id=2287 田忌赛马大家都听过,可是如果不是上中下三等马,而是很多匹马,优劣有很多种分类,就不仅仅是321的问题了. 这个很明显就是 ...

  8. NYOJ - 非洲小孩(贪心)

    http://nyoj.top/problem/1036 内存限制:64MB 时间限制:1000ms 题目描述: 家住非洲的小孩,都很黑.为什么呢? 第一,他们地处热带,太阳辐射严重. 第二,他们不经 ...

  9. 洛谷P1650:田忌赛马 ← 贪心算法

    [题目来源] https://www.luogu.com.cn/problem/P1650 [题目描述] 我国历史上有个著名的故事: 那是在2300年以前.齐国的大将军田忌喜欢赛马.他经常和齐王赛马. ...

  10. 田忌赛马贪心算法_贪心算法之田忌赛马

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

最新文章

  1. 【IntelliJ IDEA】tomcat启动,打印日志乱码问题
  2. 如何防止XshellPortable、putty、SecureCRT等断网造成Linux命令中断
  3. 玩转mini2440开发板之【编译烧录rootfs根文件系统全过程记录】
  4. 1+X web中级 Laravel学习笔记——视图和模型
  5. python2.7是什么_python2.7是什么
  6. 傻瓜看完都可以简单使用Git
  7. Env:Cscope安装与配置
  8. mouseenter和mouseleave跟mouseover和mouseout
  9. 2022蓝牙耳机哪款更值得买?最受欢迎的十大蓝牙耳机排行榜
  10. 大数据在推荐系统中的作用
  11. 从PLC ,PAC ,到施耐德的自动化开放系统
  12. [Java实验 5] 异常处理
  13. 测试hadoop服务器NodeManager无法连接ResourceManager问题
  14. 教你如何提取别人QQ头像的图片
  15. u盘容量变小了是什么原因?怎么恢复数据?
  16. SAP-QM中途取消检验类型+质检变成非限制
  17. 计算机工作功率,电脑功率(电脑一天要用多少电)
  18. 【FPGA学习笔记】(一)什么是FPGA?和单片机ARM有什么区别?
  19. 二战风云怎么修改服务器,二战风云教你玩转主城建设 详细介绍
  20. 8.OpenGL学习之颜色混合

热门文章

  1. python安装numpy-Python使用pip安装Numpy模块
  2. 编程软件python下载-python 2.7官方版
  3. python编程工资-2019年Python就业薪资怎么样?看完你就了解了
  4. python使用界面-用python编写简单ui界面窗口
  5. python到底可以做什么-编程小白必看!Python到底能做什么?
  6. python代码基础题-Python初学者福利 完整试题附答案 干货(收藏篇)
  7. python横向柱状图-python画柱状图--不同颜色并显示数值的方法
  8. python写web难受-(2017)你最不建议使用的Python Web框架?
  9. python职能-高级Python开发工程师职位描述与岗位职责任职要求
  10. 用python画太阳花-Python画太阳花