杭州电 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 (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18058 Accepted Submission(s): 5239
"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.
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.
3
92 83 71
95 87 74
2
20 20
20 20
2
20 19
22 18
0
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(贪婪)的更多相关文章
- 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
Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdoj 1052 Tian Ji -- The Horse Racing【田忌赛马】 【贪心】
思路:先按从小到大排序, 然后从最快的開始比(如果i, j 是最慢的一端, flag1, flag2是最快的一端 ),田的最快的大于king的 则比較,如果等于然后推断,有三种情况: 一:大于则比較, ...
- 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.如果田忌最慢的马比国王最慢的马慢,就 ...
- hdu1052 Tian Ji -- The Horse Racing 馋
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1052">http://acm.hdu.edu.cn/showproblem.php ...
随机推荐
- C++ Primer 学习笔记_44_STL实践与分析(18)--再谈迭代器【下】
STL实践与分析 --再谈迭代器[下] 三.反向迭代器[续:习题] //P355 习题11.19 int main() { vector<int> iVec; for (vector< ...
- cf 323A A. Black-and-White Cube 立体构造
A. Black-and-White Cube time limit per test 1 second memory limit per test 256 megabytes input stand ...
- sitemap.xml
内部类 在类内部的类 1.解决多继承 2.解决继承和实现接口时候方法名冲突情况 3.实现数据隐藏 只有内部类可以拥有4种访问修饰符 当内部类为private的时候,有外部类提供方法来访问内部类 常规 ...
- python学习笔记之二:使用字符串
这里会介绍如何使用字符串格式化其他的值,并了解一下利用字符串的分割,连接,搜索等方法能做些什么. 1.基本字符串操作 所有标准的序列操作(索引,分片,乘法,判断成员资格,求长度,取最大值和最小值)对字 ...
- 6月27日CTO俱乐部下午茶印象
作者:朱金灿 来源:http://blog.csdn.net/clever101 感谢CSDN的邀请,有幸参加了6月27日“CTO俱乐部下午茶时光:CTO在团队管理中所遇到的那些事”活动.本期的主讲嘉 ...
- Left Menu抽屉效果ScrollView姿态共存冲突
我们有一个小项目,需要做主页ScrollView嵌套TableView. 再就是Left与Right抽屉效果. 课前有眼似的,然后完成忘. 非常糟糕的记忆.真应了那句话:好记性不如烂博客. 由于我首页 ...
- Nutch+Lucene搜索引擎开发实践
网络拓扑 图 1 网络拓扑图 安装Java JDK 首先查看系统是否已经安装了其它版本号的JDK,假设有,先要把其它版本号的JDK卸载. 用root用户登录系统. # rpm-qa|grep gcj ...
- Android源码及SDK国内镜像下载
Android源码及SDK国内镜像下载Android源码下载: 今天发现,清华大学提供AOSP镜像,以前都是从Google的站点下载同步更新的,但是现在有了国内的镜像站点就好多了,下载Androidd ...
- Case when 的使用方法
SQL Case when 的使用方法 Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THE ...
- android 网络运营商的名字显示规则(锁定屏幕,下拉列表)
一:Background & 有关flow MTK Operator name display分为两种类型的手机: 1. Sim卡名称: 从基于引导SIM卡读取IMSI到Spn-conf.xm ...