http://acm.hdu.edu.cn/showproblem.php?

pid=1052

Tian Ji -- The Horse Racing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 18058    Accepted Submission(s): 5239

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

AC代码:

<span style="font-size:24px;">#include<iostream>
#include<algorithm>
using namespace std;
int T[1005],K[1005]; bool cmp(int x,int y)
{
return x>y;
} int main()
{
int n;
while(cin>>n&&n)
{
int i,ti,tj,ki,kj,grade=0;
for(i=0;i<n;i++)
cin>>T[i];
for(i=0;i<n;i++)
cin>>K[i];
sort(T,T+n,cmp);
sort(K,K+n,cmp);
ti=ki=0;
tj=kj=n-1;
for(i=0;i<n;i++)
{
if(T[ti]>K[ki])//假设田忌最快的马比齐王最快的马快,直接比。 {
ti++;
ki++;
grade+=200;
}
else if(T[ti]<K[ki])//假设田忌最快的马比齐王的慢。就拿田忌最差的马跟齐王最快的马比。 {
tj--;
ki++;
grade-=200;
}
else {
if(T[tj]>K[kj])//假设田忌最慢的马比齐王最慢的马快,直接比。
{
tj--;
kj--;
grade+=200;
}
else if(T[tj]<K[kj])//假设田忌最慢的马比齐王最慢的还慢,拿这马跟齐王最快的比。
{
tj--;ki++;
grade-=200;
}
else
{
if(T[tj]<K[ki])//田忌最慢的比齐王最快的慢。直接比。
{
tj--;
ki++;
grade-=200;
}
}
}
}
cout<<grade<<endl;
}
return 0;
}
</span>

版权声明:本文博客原创文章。博客,未经同意,不得转载。

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

    Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

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

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

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

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

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

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

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

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

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

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

  9. hdu1052 Tian Ji -- The Horse Racing 馋

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

随机推荐

  1. C++ Primer笔记10_运算符重载_赋值运算符_进入/输出操作符

    1.颂值运营商 首先来福值运算符引入后面要说的运算符重载.上一节说了构造函数.拷贝构造函数:一个类要想进行更好的控制.须要定义自己的构造函数.拷贝构造函数.析构函数.当然,还有赋值运算符.常说的三大函 ...

  2. 树上点对统计poj1741(树的点分治)

    给定一棵树,边上有权值,要统计有多少对点路径的权值和<=k 分治算法在树的路径中的应用 这个论文里面有分析. 任意两点的路径,要么过根结点,要么在子树中.如果在子树中,那么只要递归处理就行了. ...

  3. atitit.标准时间格式 相互转换 秒数 最佳实践

    atitit.标准时间格式 相互转换 秒数 最佳实践 例如00:01:19 转换为秒数  79,,and互相转换 一个思路是使用div 60 mod...只是麻烦的... 更好的方法是使用stamp ...

  4. Silverlight的Socket通信

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPwAAAGwCAIAAAACJJ+TAAAgAElEQVR4nO2deXgT5534Rdhskme7+9

  5. SessionA和pplication网上聊天室的网络范例

    login.aspx码,如以下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" ...

  6. Ubuntu下搭建ASP.NET 5

    在Ubuntu下搭建ASP.NET 5开发环境   0x00 写在前面的废话 年底这段时间实在太忙了,各种事情都凑在这个时候,没时间去学习自己感兴趣的东西,所以博客也好就没写了.最近工作上有个小功能要 ...

  7. java.lang.ClassFormatError

    Error occurred during initialization of VMjava.lang.ClassFormatError: Unknown constant tag 26 in cla ...

  8. Duanxx的Design abroad: C++矩阵运算库Eigen 概要

    一.概要 这两天想起来要做神经网络的作业了,要求用C++完毕神经网络的算法. 摆在面前的第一个问题就是,神经网络算法中大量用到了矩阵运算.可是C++不像matlab那样对矩阵运算有非常好的支持.本来准 ...

  9. 手提wifi双卡配置+window7同时多用户远程+有些公司限制网络环境方案

    该公司只提供几台机器,同时限制并连接到内部办公网络的机, 我们更多的临时工作人员,项目紧张,而另一种是太麻烦了申请, 当被问及其他网络管理,说没有变通方法. 在我的尝试,最后,找到一个解决方案; 解决 ...

  10. Android 角色时间戳

    我是在用MediaRecorder进行录像时发生视频和音频不同步的问题,请教了一些人后感觉应该是没有时间戳,之前一直觉得时间戳就是给用户看的一个数据,查了一下发现不是的,以下是转载的.希望对大家实用: ...