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. 用 Graphviz+pvtrace 可视化函数调用

    最近在想怎么把一个程序的函数调用关系快速的用流程图的方式画出来,之后看到了这个一篇文章“用 Graphviz 可视化函数调用”(http://www.ibm.com/developerworks/cn ...

  2. iOS-Runtime知识点整理

    本文目录 1.Runtime简介 2.Runtime相关的头文件 3.技术点和应用场景 3_1.获取属性\成员变量列表 3_2.交换方法实现 3_3.类\对象的关联对象,假属性 3_4.动态添加方法, ...

  3. windows 安装MySql

    转载:http://blog.csdn.net/longyuhome/article/details/7913375 Win7系统安装MySQL5.5.21图解 大家都知道MySQL是一款中.小型关系 ...

  4. 00 LabVIEW中类的动态类型处理

    1.父类使用自己的Method,连线默认为父类自己的控件 2.如果子类没有重写父类的Method,则子类使用父类方法时,直接创建出来即自己的数据类型   3.如果子类重写了父类的Method,则子类使 ...

  5. HDU1010 DFS+剪枝

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. Sprint会议记录(第五组)

    会议时间:12/8 下午14:00 会议地点:宿舍 会议进程: *首先我们讨论了实验第一个Sprint1要实现的功能:排球规则分析.比赛详细过程.比赛人物分析, *之后对是任务的认领, *最后每个人对 ...

  7. Linux下安装搭建WordPress网站

    WordPress简介 WordPress 是一种使用 PHP语言和 MySQL数据库开发的开源.免费的Blog(博客,网志)引擎,用户可以在支持 PHP 和 MySQL 数据库的服务器上建立自己的 ...

  8. Ajax调用Conrtoller返回数据

    前端ajax function doRefund(){ $.ajax({ type: "POST", catch: false, url: "@Url.Action(&q ...

  9. js 打印

    关于js打印很简单的一段代码 function doPrint() { var newWindow = window.open("打印窗口", "_blank" ...

  10. mysql_索引原理及优化

    思考: 我们知道mysql最好的数据存储量级是百万级别,是的往往在百万级别或者几十万级别就会出现慢查询(我对慢查询的定义是大于1秒),几年前我所在的一个做pos机支付的联机交易的核心系统组,当时就做过 ...