Hdu 1052 Tian 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): 18291 Accepted Submission(s):
5327
"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.
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.
number, which is the maximum money Tian Ji will get, in silver
dollars.
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1005
int tian[N],king[N]; int cmp(int a,int b)
{
return a>b;
}
int main()
{
int n, i, j;
int counts, i1, j1;
while(cin>>n && n)
{
for(i=; i<n; i++)
cin>>tian[i];
for(i=; i<n; i++)
cin>>king[i];
sort(tian,tian+n,cmp);
sort(king,king+n,cmp);
counts = ;
i = i1 = ;
j = j1 = n-;
while(i<=j)
{
if(tian[i]>king[i1]) //田忌最快的马比齐王最快的马快
{
counts++;
i++;
i1++;
}
else if(tian[i]<king[i1]) //田忌最快的马比齐王最快的马慢
{
counts--;
j--;
i1++;
}
else
{
if(tian[j]>king[j1]) //田忌最慢的马比齐王最慢的马快
{
counts++;
j--;
j1--;
}
else if(tian[j]<king[j1]) //田忌最慢的马比齐王最慢的马慢
{
counts--;
j--;
i1++;
}
else
{
if(tian[j] != king[i1]) //田忌最慢的马和齐王最快的马不相等
counts--;
j--;
i1++;
}
}
}
cout<<counts*<<endl;
}
return ;
}
Hdu 1052 Tian Ji -- The Horse Racing的更多相关文章
- HDU 1052 Tian Ji -- The Horse Racing (贪心)(转载有修改)
Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 1052 Tian Ji -- The Horse Racing【贪心在动态规划中的运用】
算法分析: 这个问题很显然可以转化成一个二分图最佳匹配的问题.把田忌的马放左边,把齐王的马放右边.田忌的马A和齐王的B之间,如果田忌的马胜,则连一条权为200的边:如果平局,则连一条权为0的边:如果输 ...
- hdu 1052 Tian Ji -- The Horse Racing (田忌赛马)
Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 1052 Tian Ji -- The Horse Racing(贪心)(2004 Asia Regional Shanghai)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 Problem Description Here is a famous story in Ch ...
- hdu 1052 Tian Ji -- The Horse Racing【田忌赛马】
题目 这道题主要是需要考虑到各种情况:先对马的速度进行排序,然后分情况考虑: 1.当田忌最慢的马比国王最慢的马快则赢一局 2.当田忌最快的马比国王最快的马快则赢一局 3.当田忌最快的马比国王最快的马慢 ...
- HDU 1052 Tian Ji -- The Horse Racing(贪心)
题目来源:1052 题目分析:题目说的权值匹配算法,有点误导作用,这道题实际是用贪心来做的. 主要就是规则的设定: 1.田忌最慢的马比国王最慢的马快,就赢一场 2.如果田忌最慢的马比国王最慢的马慢,就 ...
- 杭州电 1052 Tian Ji -- The Horse Racing(贪婪)
http://acm.hdu.edu.cn/showproblem.php? pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS ...
- hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】
思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較, ...
- hdu1052 Tian Ji -- The Horse Racing 馋
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1052">http://acm.hdu.edu.cn/showproblem.php ...
随机推荐
- Linux CentOS中使用SQL*Plus启动和关闭数据库
启动和关闭数据库的常用工具有三个 一.SQL*Plus 在SQL*Plus环境中,用户以SYSDBA身份连接到Oracle后,可以通过命令行方式启动或关闭数据库. 二.OEM(企业管理器) 利用OEM ...
- 理解margin
margin可以改变容器的尺寸 //元素尺寸分为可视尺寸,占据尺寸 margin与可视尺寸 1.适用于没有设定width/height的普通block水平元素 2.只适用于水平方向的尺寸 应用:一侧定 ...
- vmware克隆虚拟机后配置网络
一件配置: rm -rf /etc/udev/rules.d/70-persistent-net.rules cd /etc/sysconfig/network-scriptsrm -rf ifcfg ...
- jQuery实现的简单小功能(实用)
1.返回顶部使用JQuery的animate和scrollTop方法可以创建简单地返回顶部的动画: // Back to top $('#top').click(function (e) { e.pr ...
- 利用activeX控件在网页里自动登录WIN2003远程桌面并实时控制
首先要自己配置并打开受控端的WEB远程桌面服务,这个在“添加/删除windows组件”里有,我只在windows 2003 server里试过,没试过XP.下面我们在客户端安装微软提供的远程桌面客户端 ...
- Matlab的标记分水岭分割算法
1 综述 Separating touching objects in an image is one of the more difficult image processing operation ...
- Nginx 和 Apache 开启目录浏览功能
1.Nginx 在相应项目的 Server 段中的 location 段中,添加 autoindex on.例如: server { listen ; server_name www.dee.prac ...
- SQL常用字段类型
中文常用字段类型 1. 名称类 nvarchar(10) 2. 数量 int 3. 时间 date 4. 货币 money 5. 编号 ...
- php 文件日志类
php文件日志类,按年月日组织目录结构. <?php class FileLog { private $_filepath; //文件路径 private $_filename; //日志文件名 ...
- 引用log4j.jar包后,出现告警
问题现象:在引用log4j包后,使用自己导出的jar包,编译测试例代码,在启动浏览器时出现以下告警:log4j:WARN No appenders could be found for logger ...