【策略】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 ...
随机推荐
- 分享iOS最喜欢的技巧和提示
转自:http://blog.csdn.net/biggercoffee/article/details/50394027 Objective-C 1.让Xcode的控制台支持LLDB类型的打印 这有 ...
- c语言中register类型的变量
C语言中: 一.register变量 关键字regiter请求编译器尽可能的将变量存在CPU的寄存器中.有以下几点注意的地方. register变量必须是能被CPU寄存器所接受的类型,这通常意味着re ...
- codeforces 630J Divisibility
J. Divisibility time limit per test 0.5 seconds memory limit per test 64 megabytes input standard in ...
- Codeforces 622B The Time 【水题】
B. The Time time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Java中的Filter过滤器
Filter简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,Web开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件 ...
- request,response,session
1.request.getParameter("key")接受的是来自客户登陆端的数据,接受的是post或get方式传送的value. 2.请求的默认字符集是ISO-8859-1, ...
- sql2008“备份集中的数据库备份与现有数据库不同”解决方法
因为是在另一台电脑对同名数据库做的备份,用常规方法还原,提示不是相同数据库,不让还原,在网上找到下面的方法解决了: 一.右击系统数据库master,新建查询 执行以下SQL代码: RESTORE DA ...
- Swift学习笔记三
协议和扩展 在Objective-C中,协议是很常见也非常重要的一个特性,Swift中也保留了协议,语法略有变化. 用protocol关键字声明一个协议: protocol ExampleProtoc ...
- QM课程03-采购中的质量管理
QM模块被包含于采购过程的下列决策制定阶段:查询.供应商选择.采购订单.货物订单.收货.收到检查和收货数量的下达. 供应商下达 质量部门为一种被指定的物料下达一个供应商,它可以限制或限定下达的数量.如 ...
- pjsip视频通信开发(上层应用)之拨号键盘下部份拨号和删除功能
我们开发的是视频电话,所以既可以视频通话,可以只有音频的通话,所以底部含有两个按钮,最后一个就是删除功能,如果输入错误,我们可以删除输入的内容. 这里我们要通过重写LinearLayout来实现这部份 ...