D - D    田忌赛马   解题报告

hdu 1052 Tian Ji -- The Horse Racing

链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/D

题目:

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
         
题意:
田忌赛马问题,求田忌最多可以赢多少钱,即求田忌的最多净胜场数。多组案例,以0结束。
 
分析:
贪心。tq:田忌最快的马   ts:田忌最慢的马
         kq:齐王最快的马   ks:齐王最慢的马
 贪心策略:
一. tq > kq   -->  tq  VS  kq
二. tq < kq   -->  ts  VS  kq
三. tq == kq  --> 1. ts > ks  -->  ts VS ks
                          2. ts < ks  -->  ts VS kq
                          3. ts==ks  -->  ts VS kq
通过贪心就能求出田忌最多赢得的钱数。
 
代码:
 #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=; int a[maxn],b[maxn]; int main()
{
int n;
while(scanf("%d",&n)&&n)
{
for(int i=;i<n;i++)
scanf("%d",&a[i]);
for(int i=;i<n;i++)
scanf("%d",&b[i]);
sort(a,a+n);
sort(b,b+n);
int tq=n-,ts=,kq=n-,ks=;
int sum=;
for(int i=;i<n;i++)
{
if(a[tq]>b[kq])
{
sum+=;
tq--;
kq--;
}
else
{
if(a[tq]<b[kq])
{
sum-=;
ts++;
kq--;
}
else
{
if(a[ts]>b[ks])
{
sum+=;
ts++;
ks++;
}
else
{
if(a[ts]<b[kq])
sum-=;
ts++;
kq--;
}
}
}
}
printf("%d\n",sum);
}
return ;
}
 

D - D 田忌赛马的更多相关文章

  1. nyoj 364 田忌赛马(贪心)

    田忌赛马 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Here is a famous story in Chinese history. "That ...

  2. [codevs2181]田忌赛马

    [codevs2181]田忌赛马 试题描述 中国古代的历史故事"田忌赛马"是为大家所熟知的.话说齐王和田忌又要赛马了,他们各派出N匹马,每场比赛,输的一方将要给赢的一方200两黄金 ...

  3. ACM 田忌赛马

    田忌赛马 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Here is a famous story in Chinese history. "That ...

  4. TYVJ P1048 田忌赛马 Label:dp

    描述     中国古代的历史故事“田忌赛马”是为大家所熟知的.话说齐王和田忌又要赛马了,他们各派出N匹马,每场比赛,输的一方将要给赢的一方200两黄金,如果是平局的话,双方都不必拿出钱.现在每匹马的速 ...

  5. HDUOJ-------1052Tian Ji -- The Horse Racing(田忌赛马)

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

  6. HDOJ-1052 田忌赛马(贪心)

    田忌赛马 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述: Here is a famous story in Chinese history. "That was ...

  7. [洛谷P1650] 田忌赛马

    贪心难题:总结贪心问题的一般思路 传送门:$>here<$ 题意 田忌和齐王各有n匹马,赛马时一一对应.赢+200,输-200,平+0. 问最多多少钱? 数据范围:$n \leq 2000 ...

  8. Python3-大魔王小项目-田忌赛马

    本人今天第一次接触项目,花了4小时,不包括学习时间,特此留个纪念 记录一下那些年走过的坑,以资鼓励 英语不怎么好,随缘看看 内容: 类似田忌赛马,三盘两胜,属性人物在一定范围内随机,就这样了 code ...

  9. python田忌赛马

    一,简介 田忌赛马的故事大家都知道我就不展开说了,田忌能用同全面被碾压的马赢了齐威王(公子),我觉得这是十分具有智慧的.但是,如果说这里的条件改为:1,田忌的马比齐威王同等次的马弱一点但是比齐威王下一 ...

随机推荐

  1. 在Ubuntu 11.10工具栏上用数字显示网速、CPU负荷和内存占用量『译』

    基本上照抄了<How To Display Network Upload / Download Speed On The Panel In Ubuntu 11.04>,只不过我的实践环境是 ...

  2. python进阶1--数据库支持

    数据库支持 1.连接和游标 1)connect函数,该函数有多个参数,而具体使用那个参数取决于数据库.--连接数据库 常用参数: dsn:数据源名称 user:用户名 password:用户密码 ho ...

  3. python基础学习笔记5--对象

    对象(object) 1.对象(object): 面向对象程序设计重要术语. 对象的特性:多态性.封装性.继承性 >>def add(x,y): return x+y 对于很多类型的参数都 ...

  4. easyui-layout中的收缩层无法显示标题问题解决

    先看问题描述效果图片: 如上,我的查询条件是放在layout下面的一个可收缩层中,初始是收缩的,title显示不出来的话对使用者很不方便,代码如下: <div id="__MODULE ...

  5. Objective-C分类 (category)和扩展(Extension)

    1.分类(category) 使用Object-C中的分类,是一种编译时的手段,允许我们通过给一个类添加方法来扩充它(但是通过category不能添加新的实例变量),并且我们不需要访问类中的代码就可以 ...

  6. 不允许在单例对象中创建Srping容器

    spring.net在使用的时候,不允许在单例对象中创建Srping容器 需要将实例化模式转为单例singleton=“false”

  7. xcode 资源管理

    我个人觉得这么理解就够了 其他的以后再说

  8. Problem A Where is the Marble?(查找排序)

    题目链接:Problem A 题意:有n块大理石,每个大理石上写着一个非负数,首先把数从小到大排序,接下来有Q个问题,每个问题是是否有某个大理石上写着x,如果有,则输出对应的大理石编号. 思路:先排序 ...

  9. BZOJ 1058: [ZJOI2007]报表统计( 链表 + set )

    这种题用数据结构怎么写都能AC吧...按1~N弄个链表然后每次插入时就更新答案, 用set维护就可以了... --------------------------------------------- ...

  10. 我用过的Linux命令--关闭防火墙

    关闭防火墙: 防火墙的弄能是限制某一些端口的使用,可以通过linux命令关系它,相应的指令: 查看防火墙信息: #service iptables status 就能看到防火墙的状态: 关闭防火墙: ...