HDUOJ-------1052Tian Ji -- The Horse Racing(田忌赛马)
Tian Ji -- The Horse Racing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17221 Accepted Submission(s): 4998
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
Source
引用了古代田忌赛马的故事,不过此题将这个故事的三马,扩充到了n配马,面对这样一个比赛,解题方法多样。
方法一:
我们不妨先让这些马按从小到大的顺序排序,比如样列中的数据一:
3
92 83 71 ---》 71 83 92
95 87 74 74 87 95
由于得到了有序的马匹,所以只需要逐步将将角标逐步的挪一下然后比较这个过程的最大值即为所求
答案:因而我们不妨,设置一个循环数组什么的,然后就可以模拟上面的东西了..
代码如下:
当然可以使用mode来节约空间开销,但是明显增大了时间开销!!!(所以不推荐)
1 /*coder hdu 1055@Gxjun*/
2 #include<cstdio>
3 #include<cstring>
4 #include<cstdlib>
5 #include<iostream>
6 #include<algorithm>
7 using namespace std;
8 const int maxn=1005;
9 int aa[2*maxn],bb[maxn];
10 int main()
11 {
12 int n,i,j,count,cnt;
13 while(scanf("%d",&n)!=EOF&&0!=n){
14 for(i=0;i<n;i++) scanf("%d",aa+i);
15 for(i=0;i<n;i++) scanf("%d",bb+i);
16 sort(aa,aa+n); //从大到小排序 general
17 sort(bb,bb+n); //从小到大排序 king
18 for (i=0;i<n;i++) aa[i+n]=aa[i];
19 count=-0x3f3f3f3f;
20 for(i=0;i<n;i++){
21 cnt=0;
22 for(j=0;j<n;j++){
23 if(aa[i+j]<bb[j]) cnt--;
24 else if(aa[i+j]>bb[j]) cnt++;
25 }
26 if(count<cnt) count=cnt;
27 }
28 printf("%dn",count*200);
29 }
30 return 0;
31 }
对于这道题可以用二分匹配,但是tle ,面对这一个问题,我们还可以使用贪心什么的求解,最终得到我们所需要的答案!
反正总之一句话,如果田忌的慢马比国王的慢马快,直接比较,否则这个慢马 毫无价值,用来当炮灰,去跟国王的快马相比,如果田忌的快马比国王快,直接比较 否则这个快马也是个炮灰,只好去跟国王后边的慢马比,但是到底跟那个比可不一定, 所以不用去一个一个的试,当快马比不过的时候,用慢马去当炮灰,然后用快马跟国王 的下一匹快马比较
- 网站源文件被注入了iframe代码—ARP欺骗的木马病毒攻击
- ASP.NET MVC5+EF6+EasyUI 后台管理系统--工作流演示截图
- 基于CPPN与GAN+VAE生成高分辨率图像
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(54)-工作流设计-所有流程监控
- (收藏)搭建.NET Framework 3.0开发环境 及SharePoint 2007/WSS 3环境
- WCF技术剖析之八:ClientBase<T>中对ChannelFactory<T>的缓存机制
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(48)-工作流设计-起草新申请
- 把windows2003“搬”到手机上。
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(53)-工作流设计-我的批阅
- ASP.NET MVC5+EF6+EasyUI 后台管理系统--任务调度系统解析
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(52)-美化EasyUI皮肤和图标
- ASP.NET MVC5+EF6+EasyUI 后台管理系统--系统权限全套完整图
- 互联网时代的产品升级和用户反馈故事
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(50)-Easyui 扁平化皮肤
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 在Java Web中设计的编解码
- 怎么让用一行代码实现页面的定时强制刷新?脚本刷流量再也不用愁了!
- 什么是Javac
- springBoot 入门(二)—— 使用 spring.profiles.active来区分配置
- Kubernetes 1.19.0——deployment(2)
- JavaWeb新手进阶经典项目 & 半小时高效开发 & 海量知识点涵盖 (二)
- Java web 开发 Session超时设置
- JavaWeb第四讲 会话跟踪技术HttpSession、Cookie、url、隐藏表单域
- springBoot 入门(三)—— 使用 RestController
- SSM第一讲 Spring概述和基础知识详解
- springBoot 入门(四)—— 使用 纯注解方式的junit整合测试
- ClassLoader工作机制
- SSM第三讲 SpringAOP开发
- 如何加载Class文件到JVM
- spring + maven 实现发送邮件