田忌赛马

时间限制: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

题意:就是田忌赛马的故事,不过这次比赛的马不再是3只了,而变成了很多只,如果田忌输了就要给别人200块,赢了就拿200块,平局就算了。问田忌最多能拿多少钱。

思路:考虑这道题的话,就是一道标准的贪心,如何实现呢? 我们这样考虑;

1、我们先比较能力最大的,如果T(田忌)的马能力强,那就继续从强的这边比较。

2、不然就开始比较弱马,如果T弱马能力更弱或者等于K的弱马,就让这只马和K的强马比赛,田忌故意输一局。

AC代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
int win,los,t[1010],k[1010];
int n;
void cmp(){win=0;los=0;int l1=0,l2=0,r1=n-1,r2=n-1;while(l1!=r1+1){//如果直接在l1==r1的时候就跳出了循环,就会少比较一个。 if(t[r1]>k[r2]){win++;r1--;r2--;continue;//加这个是让他一次只比较一个。也可以让他一直比较然后在左右两侧相等时跳出。 }if(t[r1]<=k[r2]){if(t[l1]>k[l2]){win++;l1++;l2++;continue;//与上面的同理。 }if(t[l1]<=k[l2]){if(t[l1]<k[r2])los++;if(t[l1]>k[r2])win++;l1++;r2--;}}}
}
int main(){while(scanf("%d",&n)!=EOF){int i;for(i=0;i<n;i++)scanf("%d",&t[i]);for(i=0;i<n;i++)scanf("%d",&k[i]); sort(t,t+n);sort(k,k+n);cmp();int ans=(win-los)*200;printf("%d\n",ans); }return 0;
} 

NYOJ--364--田忌赛马相关推荐

  1. NYOJ 364 田忌赛马

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

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

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

  3. NYOJ 30 Gone Fishing JAVA+解析

    Gone Fishing这道题目运用的多次折合成一次这种思想我首次见,我想的一个思路是,每次算一下鱼量和时间代价比,这个代码我没有敲,下面的代码是一位仁兄敲得,我研读了一下,做了一个注释,应该有利于后 ...

  4. Manacher算法 , 实例 详解 . NYOJ 最长回文

    51 Nod http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1089 Manacher 算法 定义数组 p[i]表示以i为 ...

  5. NYOJ 527 AC_mm玩dota

    AC_mm玩dota 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 大家都知道AC_mm比较喜欢玩游戏,特别是擅长war3这款经典游戏.某天AC_mm来到了VS平台上 ...

  6. ctb伺服驱动器说明书_青岛FANUC伺服电机364、453故障维修

    青岛FANUC伺服电机364.453故障维修描述:青岛一家使用FANUC180i-MB系统的伺服机床,其Z轴伺服电机经常出现364/453故障报警信息,查看FANUC伺服使用说明书中关于364故障报警 ...

  7. hdu-2204 Eddy's爱好 nyoj 526

    hdu : http://acm.hdu.edu.cn/showproblem.php?pid=2204 nyoj :  http://acm.nyist.net/JudgeOnline/proble ...

  8. NYOJ 762 第k个互质数(二分 + 容斥)

    第k个互质数 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 两个数的a,b的gcd为1,即a,b互质,现在给你一个数m,你知道与它互质的第k个数是多少吗?与m互质的数按 ...

  9. NYOJ 257 中缀表达式表示成后缀表达式

    话说这道题代码那个丑陋啊,,写出来我自己都不想再看第二遍啊...看了看聪神的代码,还消耗我3个NYOJ币啊,,更扯得是,聪神的代码我看不懂啊,,,,卧槽...这道题不再多说了,数据结构上有详细的介绍, ...

  10. 364 页 PyTorch 版《动手学深度学习》PDF 开源了(全中文,支持 Jupyter 运行)

    点击上方"AI有道",选择"星标"公众号 重磅干货,第一时间送达 李沐,亚马逊 AI 主任科学家,名声在外!半年前,由李沐.Aston Zhang 等人合力打造 ...

最新文章

  1. 多组测试数据求最大值
  2. 推荐五篇论文| 轻量级的Transformer; 对比学习;ResNeSt;Shortcut Learning等
  3. PHP 接口开发注意事项
  4. C++动态(显式)调用 C++ dll示例
  5. html引入latex,如何在html文件中使用MathJax或LaTex?
  6. 从头开始编写一个时间序列数据库
  7. 05:整数序列的元素最大跨度值
  8. 【LeetCode 327】区间和的个数
  9. SQL:PostgreSQL+PostGIS的安装以及C# GDAL开发环境配置
  10. vue json对象转数组_vue的数据驱动原理及简单实现
  11. 植物大战僵尸修改植物攻击力
  12. chrome主页被毒霸网址大全劫持解决办法
  13. Android 11 状态栏电池图标的定制
  14. 2021-2027中国高效空气过滤器市场现状及未来发展趋势
  15. python爬虫beta版之抓取知乎单页面回答(low 逼版)
  16. 软件工程和计算机科学考公务员,软件工程可以考公务员吗
  17. 思科防火墙解析(ASA)
  18. html 中箭头的代码,HTML中利用div+CSS实现简单的箭头图标的代码
  19. Google Chrome Helper CPU占用过高的解决办法
  20. Java程序设计——模拟行星运动

热门文章

  1. [论文笔记]Augmented SBERT: Data Augmentation Method for Improving Bi-Encoders for Pairwise Sentence
  2. 栈和队列的Java实现
  3. 《监控》这本书让我爱不释手
  4. 3.1. 一元、多元逻辑回归、tensorflow2实现——python实战
  5. 人脸检测-Haar分类器方法
  6. 【论文阅读】Automated quantification of white matter lesion in magnetic resonance imaging
  7. r语言kendall协和系数_多变量Kendall协和系数检验.ppt
  8. open cv python_Open CV非常牛逼!众所周知!今天就来见识一下它究竟有多牛逼!
  9. java虚拟机的数据_Java虚拟机运行时数据区域
  10. 力扣-811 子域名访问计数