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

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
 
 贪心题:田忌赛马----田忌和齐王各有n匹马,每匹马都得比赛,赢一局赢200,输一局输200,问怎么比才能使田忌赢得的金钱最多?
 
要想赢得最多,田忌应该采取避其锋芒的策略,首先将二者的所有马匹按从大到小的顺序排列,然后采取以下规则:
如果田忌最快的马比齐王最快的马快,则pk掉,田忌赢一局;
如果田忌最快的马比齐王最快的马慢,则用田忌最慢的马pk齐王最快的马,田忌输一局;
否则的话,
  如果田忌最慢的马比齐王最慢的马快,则pk掉,田忌赢一局;
  如果田忌最慢的马比齐王最慢的马慢,则用田忌最慢的马pk齐王最快的马,田忌输一局;
  如果田忌最快的马与最慢马和齐王最快的马与最慢的马都相等,则用田忌最慢的马pk齐王最快的马。
 
 #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的更多相关文章

  1. 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 ...

  2. HDU 1052 Tian Ji -- The Horse Racing【贪心在动态规划中的运用】

    算法分析: 这个问题很显然可以转化成一个二分图最佳匹配的问题.把田忌的马放左边,把齐王的马放右边.田忌的马A和齐王的B之间,如果田忌的马胜,则连一条权为200的边:如果平局,则连一条权为0的边:如果输 ...

  3. 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 ...

  4. 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 ...

  5. hdu 1052 Tian Ji -- The Horse Racing【田忌赛马】

    题目 这道题主要是需要考虑到各种情况:先对马的速度进行排序,然后分情况考虑: 1.当田忌最慢的马比国王最慢的马快则赢一局 2.当田忌最快的马比国王最快的马快则赢一局 3.当田忌最快的马比国王最快的马慢 ...

  6. HDU 1052 Tian Ji -- The Horse Racing(贪心)

    题目来源:1052 题目分析:题目说的权值匹配算法,有点误导作用,这道题实际是用贪心来做的. 主要就是规则的设定: 1.田忌最慢的马比国王最慢的马快,就赢一场 2.如果田忌最慢的马比国王最慢的马慢,就 ...

  7. 杭州电 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 ...

  8. hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】

    思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較, ...

  9. hdu1052 Tian Ji -- The Horse Racing 馋

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1052">http://acm.hdu.edu.cn/showproblem.php ...

随机推荐

  1. Leetcode Valid Number

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  2. WCF的同步和异步(以WPF连接为例)

    2016-06-0711:05:44 在学习WCF时,学到WCF服务的同步和异步. 我理解的同步是: 当WCF服务是同步执行时,程序只有一条线程,代码只能按顺序一步一步来执行,当执行客户端/服务端某方 ...

  3. chrome浏览器 模拟访问移动端

    谷歌Chrome浏览器,可以很方便地用来当3G手机模拟器.在Windows的[开始]-->[运行]中输入以下命令,启动谷歌浏览器,即可模拟相应手机的浏览器去访问3G手机网页: 谷歌Android ...

  4. fnc.tld学习编写

    使用 el 的过程中,需要使用到后端代码处理逻辑,这个时候我们就需要自定义 方法. 如我们后端代码定义如下: package com.rhythmk.common; public class FncH ...

  5. 谢欣伦 - OpenDev原创教程 - 本地IP查找类CxLocalHostIPAddrFind

    这是一个精练的本地IP查找类,类名.函数名和变量名均采用匈牙利命名法.小写的x代表我的姓氏首字母(谢欣伦),个人习惯而已,如有雷同,纯属巧合. CxLocalHostIPAddrFind的使用如下: ...

  6. Oracle总结

    摘自:http://www.cnblogs.com/linjiqin/category/283838.html oracle decode用法 select decode( x , 1 , ‘x is ...

  7. mvc 学习(一)

    1.a标签里三元运算符的写法 : <a href="/Travel/TravelAbroadList/@ViewData["searchname"]_0_@View ...

  8. 阿里云服务器Linux CentOS安装配置(三)yum安装mysql

    阿里云服务器Linux CentOS安装配置(三)yum安装mysql 1.执行yum安装mysql命令:yum -y install mysql-server mysql-devel 2.启动mys ...

  9. ASP.NET Web API 2基于令牌的身份验证

    基于令牌的认证 我们知道WEB网站的身份验证一般通过session或者cookie完成的,登录成功后客户端发送的任何请求都带上cookie,服务端根据客户端发送来的cookie来识别用户. WEB A ...

  10. iOS10 适配、Xcode8配置总结

    随着iOS10的推送更新到来,勤劳的程序员又在加班加点的搬砖了,为此收集了一些iOS10 更新的技能给大伙参考,不断更新喜欢就star 前沿 一.Xcode8 插件你去哪了 以为是和之前一样 Xcod ...