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. POSIX扩展正则表达式函数

    1.ereg()函数和eregi()函数 函数语法: bool ereg/eregi ( string pattern, string string [, array regs] ) 函数功能: 在字 ...

  2. Latex beamer

    使用明体(gbsn)和楷体(gkai)两种字型.以下的编程是一开始的宣告,并自动生成第一张封面投影片. \documentclass[cjk]{beamer}\usepackage{CJKutf8}\ ...

  3. “-Xmx1024m -Xms1024m -Xmn512m -Xss256k”——Java运行参数(转)

    JVM的堆的内存, 是通过下面面两个参数控制的 -Xms 最小堆的大小, 也就是当你的虚拟机启动后, 就会分配这么大的堆内存给你 -Xmx 是最大堆的大小 当最小堆占满后,会尝试进行GC,如果GC之后 ...

  4. jdk outMemory内存溢出

    参数的含义: -vmargs -Xms256M -Xmx768M -Xss256k -XX:PermSize=256M -XX:MaxPermSize=768M -vmargs 说明后面是vm的参数 ...

  5. QCombobox设置下拉框的宽度

    这几天写一个项目,里面用到qcombobox组件,其中下拉框含有129个子项,所以在点击的时候,一个下拉框就将整个电脑屏幕给占满了,很不好看并且在使用中会造成很大的苦恼.其实我就是想设置一个下拉框最大 ...

  6. 多线程中Local Store Slot(本地存储槽)

    在Java中有一种ThreadLocal机制,为每一个使用该变量的线程都提供一个变量值的副本,是每一个线程都可以独立地改变自己的副本,而不会和其它线程的副本冲突.从线程的角度看,就好像每一个线程都完全 ...

  7. css版hover现边框

    需要注意的是  hover中要给盒子加:position:relative; <style type="text/css"> *{margin:0;padding:0; ...

  8. FineUI框架 使用asp.net控件及其使用问题

    FineUI 基于ExtJS的开源ASP.Net框架库--创建 No JavaScript,No CSS,No UpdatePanel,No ViewState,No WebServices 的网站应 ...

  9. Unity3d 通过协程来实现文件的全部加载后执行

    相信大家会经常遇到在游戏中需要WWW从本地或者服务器上获取数据,而我们通常容易会犯下面这种个错误:当数据较少或者网速较好时程序运行正常.而当数据较大或者网速不好时程序会出错误.比如卡住. 所以我们要使 ...

  10. hibernate -inverse

    one to many inverse=false只能设置维护关联关系的多的一方, inverse属性: 默认为false,表示本方维护关联关系. 如果为true,表示本方不维护关联关系(并不意味着对 ...