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 题意 : 就是正常的田忌赛马,赢的话会得到 200 奖金。
思路分析:将马的速度排一下序,田忌要想赢得更多的奖金,那么当他的最好的马比不上齐王最好的马时,就要用自己最差的马去和齐王去比,如果这么一想的话就是一个 dp,
  dp[i][j]表示到第 i 场比赛时,使用 j 匹差马的最大收益。
  dp[i][j] = max(dp[i-1][j]+score(a[i-j], b[i]), dp[i-1][j-1]+score(a[n-j+1], b[i]));
代码示例:
int n;
int a[1005], b[1005];
bool cmp(int x, int y){
return x > y;
}
int dp[1005][1005]; int score(int x, int y){
if (x > y) return 1;
if (x == y) return 0;
return -1;
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout); while(~scanf("%d", &n) && n){
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
}
for(int i = 1; i <= n; i++){
scanf("%d", &b[i]);
}
sort(a+1, a+1+n, cmp);
sort(b+1, b+1+n, cmp); memset(dp, 0x8f, sizeof(dp));
dp[0][0] = 0; for(int i = 1; i <= n; i++){
for(int j = 0; j <= i; j++){
if (j == 0){
dp[i][0] = dp[i-1][0]+score(a[i], b[i]);
}
else {
dp[i][j] = max(dp[i-1][j]+score(a[i-j], b[i]), dp[i-1][j-1]+score(a[n-j+1], b[i]));
}
}
}
int ans = -inf;
for(int i = 0; i <= n; i++) ans = max(ans, dp[n][i]);
printf("%d\n", ans*200);
}
return 0;
}

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

  1. TYVJ P1048 田忌赛马 Label:dp

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

  2. [POJ2287][Tyvj1048]田忌赛马 (贪心+DP)

    瞎扯 很经典的一道题 考前才打 我太菜了QAQ 就是先贪心排序了好 然后在DP 这样比直接DP更容易理解 (其实这题做法还有很多) 代码 #include<cstdio> #include ...

  3. [codevs2181]田忌赛马

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

  4. [洛谷P1650] 田忌赛马

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

  5. tyvj1048 田忌赛马

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

  6. DP小题集

    P2736 "破锣摇滚"乐队 Raucous Rockers 你刚刚继承了流行的"破锣摇滚"乐队录制的尚未发表的N(1 <= N <= 20)首歌的 ...

  7. 【dp 贪心】bzoj4391: [Usaco2015 dec]High Card Low Card

    巧妙的贪心 Description Bessie the cow is a huge fan of card games, which is quite surprising, given her l ...

  8. BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4142  Solved: 1964[Submit][Statu ...

  9. 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...

随机推荐

  1. python模块之configparser模块

    configparser模块:用于按一定格式创建配置文件 创建 import configparser config = configparser.ConfigParser() config['DEF ...

  2. ZR1158

    ZR1158 http://www.zhengruioi.com/contest/446/problem/1158 给定限制的问题大多数都是容斥或者二分,或者二分之后容斥 首先,这个问题的第一步我们还 ...

  3. CSS3侧栏滑出简单实现

    使用css3 的 animation 属性实现的点击滑出侧栏 <!DOCTYPE html> <html lang="en"> <head> & ...

  4. Linux 内核PCI去除一个驱动

    去除一个驱动是一个非常容易的动作. 对于一个 PCI 驱动, 驱动调用 pci_unregister_driver 函数. 这个函数只调用驱动核心函数 driver_unregister, 使用 一个 ...

  5. 【51.64%】【POJ 1330】Nearest Common Ancestors

    Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26416 Accepted: 13641 Description A roote ...

  6. JSR303 数据检验

    原文:https://blog.csdn.net/qq_28867949/article/category/7370730 一.JSR-303简介 JSR-303 是 JAVA EE 6 中的一项子规 ...

  7. JNDI数据源的使用

    有时候我们数据库的连接会使用jndi的方式 try { InitialContext ic = new InitialContext(); dataSource = (DataSource) ic.l ...

  8. 工厂设计模式灵魂拷问-Java实现

    show me the code and take to me,做的出来更要说的明白 GitHub项目JavaHouse同步收录 喜欢就点个赞呗! 你的支持是我分享的动力! 引入 我们经常听到工厂模式 ...

  9. node.js UDP NAT 穿透实现

    源码:https://github.com/zhongchengyi/node-udp-trunnel-demo 1.原理 A登录Server, NAT A 分配端口11000,Server得到A的地 ...

  10. APP数据采集--基础配置

    一.首先需要下载fiddler,可以直接在官网下载最新版呢 二.设置允许抓取HTTPS数据: 打开已经下载好了的fiddler,打开在工具栏中的Tools,之后在HTTPS的工具栏下面进行如下的设置 ...