【策略】UVa 1344 - Tian Ji -- The Horse Racing(田忌赛马)
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 ( n1000) 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 贪心策略:
一、当田忌最快的马比国王最快的马快时,用田忌最快的马赢国王最快的马。
二、当田忌最快的马比国王最快的马慢时,用田忌最慢的马输给国王最快的马。
三、当田忌最快的马跟国王最快的马一样快时,分情况。(对于情况三,我们应该从最慢的马开始考虑了)
1、当田忌最慢的马比国王最慢的马快,那么用田忌最慢的马赢国王最慢的马
2、当田忌最慢的马比国王最慢的马慢,那么用田忌最慢的马输给国王最快的马
3、当田忌最慢的马跟国王最慢的马相等的时候,用田忌最慢的马跟国王最快的马比(此时国王最快的马只能大于等于田忌最慢的马,若大于,则田忌输,等于,平局)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int maxn = ;
int t[maxn], k[maxn];
int main()
{
int n;
while(~scanf("%d", &n) && n)
{
for(int i = ; i < n; i++)
{
scanf("%d", &t[i]);
}
for(int i = ; i < n; i++)
{
scanf("%d", &k[i]);
}
sort(t, t+n); sort(k, k+n);
int tmax = n-, kmax = n-; //T和K最快的马的编号
int tmin = , kmin = ; //T和K最慢的马的编号
int win = , lose = ;
while(tmin <= tmax && kmin <= kmax)
{
if(t[tmax] > k[kmax])
{
tmax--; kmax--;
win++;
}
else if(t[tmax] < k[kmax])
{
tmin++; kmax--;
lose++;
}
else
{
if(t[tmin] > k[kmin])
{
tmin++; kmin++;
win++;
}
else if(t[tmin] < k[kmin])
{
tmin++; kmax--;
lose++;
}
else
{
if(t[tmin] < k[kmax])
lose++;
tmin++; kmax--;
}
}
}
printf("%d\n", ((win-lose)*));
}
return ;
}
【策略】UVa 1344 - Tian Ji -- The Horse Racing(田忌赛马)的更多相关文章
- UVA 1344 Tian Ji -- The Horse Racing
Tian Ji -- The Horse Racing Here is a famous story in Chinese history. That was about 2300 years ago ...
- 【贪心】[hdu1052]Tian Ji -- The Horse Racing(田忌赛马)[c++]
Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 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 ...
- [HDU1052]Tian Ji -- The Horse Racing(田忌赛马)
题目大意:田忌赛马问题,给出田忌和齐威王的马的数量$n$和每匹马的速度$v$,求田忌最多赢齐威王多少钱(赢一局得200,输一局扣200,平局不得不扣). 思路:贪心. 1.若田忌最慢的马可以战胜齐王最 ...
- 【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)
Tian Ji -- The Horse Racing 田忌赛马,还是English,要不是看题目,我都被原题整懵了,直接上Chinese吧 Descriptions: 田忌和齐王赛马,他们各有n匹马 ...
- 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 ...
- Tian Ji -- The Horse Racing HDU - 1052
Tian Ji -- The Horse Racing HDU - 1052 (有平局的田忌赛马,田忌赢一次得200块,输一次输掉200块,平局不得钱不输钱,要使得田忌得到最多(如果只能输就输的最少) ...
- 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 ...
随机推荐
- urllib2中自定义opener
正常用Python抓取网页信息,需要用到urllib2,调用urllib2.urlopen(url),可以获得response 反馈信息,再用response.read()即可获得页面的源码. 最简单 ...
- (转)UML序列图总结
序列图主要用于展示对象之间交互的顺序. 序列图将交互关系表示为一个二维图.纵向是时间轴,时间沿竖线向下延伸.横向轴代表了在协作中各独立对象的类元角色.类元角色用生命线表示.当对象存在时,角色用一条虚线 ...
- Spring Auto proxy creator example
In last Spring AOP examples – advice, pointcut and advisor, you have to manually create a proxy bean ...
- [转]省市二级联动(纯js实现)
转至:http://www.jb51.net/article/41556.htm 实现原理: set_city("省名称",市select对象); 判断市select对象是否为空, ...
- 【Java】Socket+多线程实现控制台聊天室
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827212.html 聊天室程序的结构图: 架构解释: Server服务器相当于一个中转站,Client客户端 ...
- centos 6.5下安装docker
关于docker的更多信息,请移步度娘.以下两个链接也对docker有了具体的介绍: http://www.docker.org.cn/book/docker/what-is-docker-16.ht ...
- 项目经验之:MVVM初学者图形化笔记整理。。。
这个模式,一下子把我的思路给打开..让我眼前一亮..居然可以这样将界面分离得如此彻底........... 大家一起学习... 说说我的感受吧,一个小实例讲述了 MVVM实现原理: 一个简单的例,将两 ...
- iOS 抖动动画
-(void)animationWithCell:(WaterLevelCollectionCell *)cell{ // 添加摇晃动画 { CAKeyframeAnimation *frame=[C ...
- Struts 2.x异常:Unable to load configuration..../WEB-INF/lib/struts2-convention-plugin-2.1.6.jar!/struts-plugin.xml:30:119
Struts 2.x异常:Unable to load configuration..../WEB-INF/lib/struts2-convention-plugin-2.1.6.jar!/strut ...
- F5 负载均衡
http://xjsunjie.blog.51cto.com/blog/999372/697285 http://www.eimhe.com/thread-142659-1-1.html