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. python strip()函数介绍

    函数原型 声明:str为字符串,s为要删除的字符序列 str.strip(s)        删除str字符串中开头.结尾处,位于 s删除序列的字符 str.lstrip(s)       删除str ...

  2. Linux内核和驱动编译常见问题

    1.获取内核源码应为 git clone depth=1  不然编译时会报错 2.有些RTL188CUS网卡与wpa_supplicant兼容有问题 更新wpasupplicant或许能解决 这里用i ...

  3. liunx使用技巧

    1.挂载与卸载U盘 新建一个目录:mkdir /mnt/usb; Fdisk –l |less  查看添加之后的设备名,设备文件系统格式 加载U盘设备: mount –t vfat /mnt/usb ...

  4. SSD的基本架构

    在SSD的优势一章中,我们对比过HDD和SSD的内部区别.现在,我们再谈一下SSD的基本架构.                   上图为一款典型的SSD架构图解,各部分的解释如下:       操作 ...

  5. Advanced Replication同步复制实验(基于Trigger&基于Materialized View)

    1. 高级复制和流复制介绍 1.1 高级复制(Advanced Replication) 高级复制也称为对称复制,分为多主体站点复制(Multiple Master Rplication).物化视图站 ...

  6. HDU 5875 Function(ST表+二分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5875 [题目大意] 给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不 ...

  7. mongoose的用法(注:连接数据库)

    第一步:连接数据库: mongoose.connect('mongodb://'+user+':'+pass+'@mongo.duapp.com:'+port+'/xzWIRHYlWLAApdsfAz ...

  8. 第七届河南省赛A.物资调度(dfs)

    10401: A.物资调度 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 95  Solved: 54 [Submit][Status][Web Bo ...

  9. loadRunner 11.0 安装及破解

    http://jingyan.baidu.com/article/20095761b31b58cb0621b463.html 破解时必须是管理员账户登录.

  10. 笔记 postgresql oid同步

    以前学习postgresql的笔记 create table 消耗 OID 如create table my_test_table, 他本身会消耗一个 会在pg_type中插入两条记录_my_test ...