田忌赛马 - 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 ...
随机推荐
- 12174 - Shuffle——[滑动窗口]
You are listening to your music collection using the shuffle function to keep the music surprising. ...
- linux ioctl 系统调用预定义的命令
尽管 ioctl 系统调用最常用来作用于设备, 内核能识别几个命令. 注意这些命令, 当用 到你的设备时, 在你自己的文件操作被调用之前被解码. 因此, 如果你选择相同的号给一 个你的 ioctl 命 ...
- javascript中的深拷贝与浅拷贝
javascript中的深拷贝与浅拷贝 基础概念 在了解深拷贝与浅拷贝的时候需要先了解一些基础知识 核心知识点之 堆与栈 栈(stack)为自动分配的内存空间,它由系统自动释放: 堆(heap)则是动 ...
- H3C tracert命令的输出
- Codeforces Round #587 C. White Sheet(思维+计算几何)
传送门 •题意 先给一个白矩阵,再两个黑矩阵 如果两个黑矩阵能把白矩阵包含,则输出NO 否则输出YES •思路 计算几何题还是思维题呢? 想起了上初中高中做几何求面积的题 这个就类似于那样 包含的话分 ...
- Flutter TextField设置默认值默认值和光标位置
主要通过controller 实现,具体代码如下 TextField( //输入键盘类型 keyboardType: TextInputType.text, autofocus: true, deco ...
- Windows 服务安装与卸载 (通过 Sc.exe)
1. 安装 新建文本文件,重命名为 ServiceInstall.bat,将 ServiceInstall.bat 的内容替换为: sc create "Verity Platform De ...
- 【Linux】Ubuntu16.04 ftp匿名访问
一.安装: 1.安装命令: sudo apt-get install vsftpd 2.配置文件: vi /etc/vsftpd.conf 3.开启.关闭.重启服务 sudo /etc/init.d/ ...
- 22.BASE_DIR,os,sys
原文: BASE_DIR演示 想在bin里调用main里的方法.需要找到目录. import sys,os BASE_DIR = os.path.dirname(os.path.dirname(os. ...
- 最小化centos7.4系统静默安装oracle12.2
一 orace简介 ORACLE(甲骨文)公司.Oracle Database (甲骨文数据库) 是一个具有对象和可扩展标记语言(XML)功能的关系数据库,提供的以分布式数据库为核心的一组软件产品,是 ...