Tian Ji – The Horse Racing(田忌赛马)

田忌赛马的故事
(可以直接看题)
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

思路:
1、先从田忌的最差的马和齐王的最差的马比,如果田忌的马速度大于齐王的马赢一把。
2、如果田忌的最差的马比齐王最差的马还菜,让田忌的最差马拼掉齐王最好的马。
3、如果田忌的最差的马比齐王最差的马一样速度,则比较田忌最好的马和齐王最好的马:
① 如果田忌最好的马比齐王最好的马快,赢一把。
②如果田忌最差的马比齐王最好的马慢,让田忌最差的马和齐王最好的马比,输一把,一换一。
③如果田忌的最好的马和齐王的最好的马一样快。让田忌最差的马把齐王的最好的马比,输一把,一换一。
AC代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
int a[10050];    //田忌的马
int b[10050];    //齐王的马
int main()
{int n;while(~scanf("%d",&n)) //多组输入{int win=0,lost=0,s=n,e=0;int sum=0;if(n==0)   //题目要求输入0,退出程序{break;}for(int i=0;i<n;i++){scanf("%d",&a[i]);}for(int i=0;i<n;i++){scanf("%d",&b[i]);}//把马的速度排序sort(a,a+n);sort(b,b+n);for(int i=0;i<n;i++){for(int j=e;j<s;j++){if(a[i]<b[j])   //田忌最慢的马比齐王最慢的慢{lost++;     //败场加一s--;break;       //跳出循环再次比较}else if(a[i]>b[j])  //田忌最慢的马比齐王最慢的快{win++;          //胜场加一e++;break;          //跳出循环再次比较}else        //田忌最慢的马和齐王最慢的马速度一样{if(a[n-1]<b[s-1])   //田忌最好的马速度小于齐王最好的马{lost++; //败场加一s--;break;}else if(a[n-1]==b[s-1]){if(a[i]<b[s-1])    //田忌最慢的马和齐王最好的比{lost++;     //败场加一s--;break;}else    //顶多可以相等大于是不可能了{s--;break;}}else             //田忌最好的马比齐王的快{win++;  //胜场加一s--;n--;i--;break;}}}}sum=(win-lost)*200;printf("%d\n",sum);}return 0;
}

Tian Ji -- The Horse Racing(田忌赛马)/贪心算法相关推荐

  1. POJ 2287 - Tian Ji -- The Horse Racing(贪心)

    题意:田忌和齐王都有 n(n <= 1000)匹马,二者进行 n 轮赛马,赢一局+200,平一局得 0,输一局 -200,求田忌最多能赢多少钱. 贪心,先将田忌和齐王的马分别排序,然后最上等与最 ...

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

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

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

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

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

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

  5. OpenJudge Tian Ji -- The Horse Racing

    目录 Tian Ji -- The Horse Racing 要求: 描述: 输入 : 输出: 样例输入: 样例输出: 问题分析: 情况一: 情况二: 情况三: 最终代码: 总结: 其他思路: Tia ...

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

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

  7. 杭电1042c语言循环,HDU杭电1052 Tian Ji - The Horse Racing答题报告

    HDU杭电1052 Tian Ji -- The Horse Racing解题报告 本人第一次写博客,希望各位大神多多指导与包涵,不足的地方还请指出,新手在此谢过啦!!! 题目描述: Time Lim ...

  8. LA 3266 Tian Ji -- The Horse Racing

    题目 LA 3266 Tian Ji – The Horse Racing 题解 这神奇的贪心我是并没有想出来,只会做没有平局的情况,感觉自己太傻比,不知道怎么冲省选了.找的是这篇题解:http:// ...

  9. hdu 1052 Tian Ji -- The Horse Racing

    Tian Ji -- The Horse Racing Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Jav ...

最新文章

  1. 网页(Webpage)粒度分析算法
  2. Java 程序员一次有趣的面试 | 每日趣闻
  3. 嵌入式设备web服务器比较
  4. 给工厂分配销售组织/分销渠道
  5. Fiddler抓包使用教程-乱码处理 Decode
  6. Linux中的进程组及会话
  7. 《Head First设计模式》第九章(2)组合模式
  8. HTML5 装饰Canvas中图形
  9. 图谱论(Spectral Graph Theory)基础
  10. Material使用05 MdListModule模块 MdButtonToggleModule模块
  11. focal loss dice loss源码_扒源码:sharding loss in Pytorch
  12. 传智播客 网络通信过程学习笔记
  13. SSM+Jedis初体验
  14. acs cisco 查看log_Cisco ASA 5510 防火墙 配置笔记
  15. oracle 联合查询去重,oracle两张表关联查询
  16. html5 h5是什么,H5和HTML的区别是什么
  17. 计算机输入网站打不开,电脑打不开网页怎么办
  18. in作为介词的用法_介词in的用法
  19. 怎么用计算机表达爱意,简单表达爱意的句子
  20. 所有用户登陆后都在桌面上显示计算机图标,如何在Win10桌面上显示计算机.控制面板.网络.用户的文件图标?...

热门文章

  1. springboot入门到入坟
  2. c语言中换行符与回车符的区别,C语言中换行符与回车符的区别
  3. 大数据开发:大数据背景下的数据库选型
  4. cocos2d 屏幕適配_cocos2d-x 屏幕适配新解
  5. 2021-2025年中国废水泵行业市场供需与战略研究报告
  6. JAVA程序猿必做算法题(45题)
  7. 哈工大软件构造 复习
  8. java基础之重写父类_繁星漫天_新浪博客
  9. 自律训练法 John Sehorz
  10. oracle中的index函数,Oracle中的索引详解(整理)