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. JQuery操作select下拉框

    JQuery操作select下拉框 获取Select选择的Text和Value $("#select_id").change(function(){//code...}); //为 ...

  2. [转]C#操作word模板插入文字、图片及表格详细步骤

    c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...

  3. linux 分配和释放设备编号

    在建立一个字符驱动时你的驱动需要做的第一件事是获取一个或多个设备编号来使用. 为 此目的的必要的函数是 register_chrdev_region, 在 <linux/fs.h>中声明: ...

  4. windows服务器运维日常--防火墙打开后ping不通

    1. 打开防火墙,有利于安全 2. 添加80端口,支持互联网访问:添加3389端口,以支持远程桌面连接 3. 发现开了防火墙之后,ping不通网址www.mjywxy.xin 4. 查找资料和测试发现 ...

  5. P1036 最大公约数

    题目描述 给你两个正整数A和B,求它们的最大公约数. 输入格式 两个正整数 \(A,B(1 \le A,B \le 10^9)\) . 输出格式 一个整数,表示A和B的最大公约数. 样例输入 6 8 ...

  6. 天河2 程序 version GLIBCXX_3.4.21 not found 解决方法

    本文告诉大家在 天河2 运行程序时发现 version GLIBCXX_3.4.21 not found 如何修复 我在天河2运行一个程序报错 version `GLIBCXX_3.4.21' not ...

  7. PHP性能监控

    使用xhprof进行线上PHP性能追踪及分析 日志未经声明,均为AlloVince原创.版权采用『 知识共享署名-非商业性使用 2.5 许可协议』进行许可. 之前一直使用基于Xdebug进行PHP的性 ...

  8. vue-learning:34 - component - 内置组件 - 动态组件component 和 is属性

    component动态组件 / is属性 让多个组件使用同一个挂载点,并动态切换,这就是动态组件. 必要条件: 组件标签使用<component></component> 动态 ...

  9. Linux 内核PCI去除一个设备

    一个 PCI 可用多个不同的方法被从系统中去除. 所有的 card-bus 设备在一个不同的物 理因素上是真正的 PCI 设备, 并且内核 PCI 核心不区分它们. 允许在机器运行时加减 PCI 设备 ...

  10. 2019-2-11-WPF-列表自动换行

    title author date CreateTime categories WPF 列表自动换行 lindexi 2019-02-11 08:55:31 +0800 2019-02-11 08:5 ...