田忌赛马 - dp
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
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
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的更多相关文章
- TYVJ P1048 田忌赛马 Label:dp
描述 中国古代的历史故事“田忌赛马”是为大家所熟知的.话说齐王和田忌又要赛马了,他们各派出N匹马,每场比赛,输的一方将要给赢的一方200两黄金,如果是平局的话,双方都不必拿出钱.现在每匹马的速 ...
- [POJ2287][Tyvj1048]田忌赛马 (贪心+DP)
瞎扯 很经典的一道题 考前才打 我太菜了QAQ 就是先贪心排序了好 然后在DP 这样比直接DP更容易理解 (其实这题做法还有很多) 代码 #include<cstdio> #include ...
- [codevs2181]田忌赛马
[codevs2181]田忌赛马 试题描述 中国古代的历史故事"田忌赛马"是为大家所熟知的.话说齐王和田忌又要赛马了,他们各派出N匹马,每场比赛,输的一方将要给赢的一方200两黄金 ...
- [洛谷P1650] 田忌赛马
贪心难题:总结贪心问题的一般思路 传送门:$>here<$ 题意 田忌和齐王各有n匹马,赛马时一一对应.赢+200,输-200,平+0. 问最多多少钱? 数据范围:$n \leq 2000 ...
- tyvj1048 田忌赛马
描述 中国古代的历史故事“田忌赛马”是为大家所熟知的.话说齐王和田忌又要赛马了,他们各派出N匹马,每场比赛,输的一方将要给赢的一方200两黄金,如果是平局的话,双方都不必拿出钱.现在每匹马的速 ...
- DP小题集
P2736 "破锣摇滚"乐队 Raucous Rockers 你刚刚继承了流行的"破锣摇滚"乐队录制的尚未发表的N(1 <= N <= 20)首歌的 ...
- 【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 ...
- BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]
1911: [Apio2010]特别行动队 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 4142 Solved: 1964[Submit][Statu ...
- 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 ...
随机推荐
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence(思维)
传送门 题意: 给你一个只包含 '(' 和 ')' 的长度为 n 字符序列s: 给出一个操作:将第 i 个位置的字符反转('(' ')' 互换): 问有多少位置反转后,可以使得字符串 s 变为&quo ...
- 2019-4-12-WPF-绑定的默认模式
title author date CreateTime categories WPF 绑定的默认模式 lindexi 2019-04-12 09:38:58 +0800 2019-04-12 09: ...
- caffe学习(1):多平台下安装配置caffe
如何在 centos 7.3 上安装 caffe 深度学习工具 有好多朋友在安装 caffe 时遇到不少问题.(看文章的朋友希望关心一下我的创业项目趣智思成) 今天测试并整理一下安装过程.我是在阿 ...
- element-ui tree 根据不同叶子节点设置是否显示复选框
公司业务要求不同根节点配置显示与否复选框,官方文档没有这样的配置,所以想到了修改element-ui源码. 1.这里将“node_modules\element-ui\packages”下的tree文 ...
- url查找参数
function GetUrlParam(paraName) { var url = document.location.toString(); var arrObj = url.split(&quo ...
- Linux USB 的 Urbs
linux 内核中的 USB 代码和所有的 USB 设备通讯使用称为 urb 的东西( USB request block). 这个请求块用 struct urb 结构描述并且可在 include/l ...
- jdk+tomcat+mysql一键安装脚本
最近在搞一个web项目部署,每次都要安装jdk.配置环境变量.安装tomcat和mysql.对于非开发人员,还是有点难度的,经常出错,然后就整理了一个自动化的脚本. JDKinstall.bat @e ...
- [板子]Kruskal
众所周知求最小生成树的两种方法: 1.Kruskal 2.Prim 这里只挂第一种,因为noip掌握第一种就够了. 两种做法的区别可以参考这个博客:http://blog.csdn.net/molln ...
- 空气质量管理系统ssm(mybatis+spring+springMVC)框架+前后端分离
1.目录结构: 2.需要注意的地方 2.1在WEB-INFO下新建 2.1.1 springMVC-servlet.xml <?xml version="1.0" encod ...
- 记一次ftp错误
在一个ftp上,突然登不上 报错,使用浏览器登录,报此用户不是私密连接,然后使用服务器客户端登录尝试,错误信息如下: [root@test ~]# ftp *.*.*.* Connected to * ...