当前位置:
文档之家› 5C案例04动态规划PPT课件
5C案例04动态规划PPT课件
W[m[1]] < W[m[2]] < ... < W[m[n]]
and
S[m[1]] > S[m[2]] > ... > S[m[n]]
In order for the answer to be correct, n should be as large as possible. All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.
line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that
10.11.2020
8
二、经典问题:最长有序子序列
10.11.2020
9
二、经典问题:最长有序子序列
ห้องสมุดไป่ตู้
10.11.2020
10
二、经典问题:最长有序子序列
10.11.2020
11
三 1160 FatMouse's Speed
Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.
第四讲
动态规划
(Dynamic programming)
10.11.2020
1
一、经典问题:数塔问题
有形如下图所示的数塔,从顶部出发,在每 一结点可以选择向左走或是向右走,一直走到底 层,要求找出一条路径,使路径上的值最大。
10.11.2020
2
用暴力的方法,可以吗?
10.11.2020
3
试想一下:
Input Input contains data for a bunch of mice, one mouse per line, terminated by end of file.
The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.
10.11.2020
7
二、经典问题:最长有序子序列
n [问题描述] 找出由n个元素组成的序列的最长有序子序列
长度及其中一个最长有序子序列 (注:这里有序指非递减顺序,且不要求子序列 连续)。 例如,对于序列[3, 7, 1, 5, 9, 3],其中最长有序 子序列长度为3,这样的子序列有: [3, 7, 9]、[1, 5, 9]、[3, 5, 9]。
10.11.2020
13
三 1160 FatMouse's Speed
Sample Input
6008 6000 500 1000 1100 6000 8000 6000 2000
1300 2100 2000 4000 3000 2000 1400 1200 1900
Sample Output
4 4 5 9 7
同样,下一层的走向又要取决于再下一层上的最 大值是否已经求出才能决策。这样一层一层推下去, 直到倒数第二层时就非常明了。
如数字2,只要选择它下面较大值的结点19前进 就可以了。所以实际求解时,可从底层开始,层层递 进,最后得到最大值。
结论:自顶向下的分析,自底向上的计算。
10.11.2020
6
思路:从倒数第二行起, 按照状态转移方程 dp[i][j] = max(dp[i + 1][j], dp[i + 1][j + 1]) + val[i][j] 向上递推, 直到val[1][1], 此时dp[1][1]就是结果
10.11.2020
14
三 1160 FatMouse's Speed
解题思路: 题目要求找到的体重递增,速度递减 老鼠,先把老鼠的体重进行升序排序 然后算出速度的最长递减子序列。
Two mice may have the same weight, the same speed, or even the same weight and speed.
10.11.2020
12
三 1160 FatMouse's Speed
n Output n Your program should output a sequence of lines of data; the first
这道题如果用枚举法(暴力思想),在 数塔层数稍大的情况下(如31),则 需要列举出的路径条数将是一个非常庞 大的数目(2^30= 1024^3 > 10^9=10亿)。
10.11.2020
4
拒绝暴力,倡导和谐~
10.11.2020
5
考虑一下:
从顶点出发时到底向左走还是向右走应取决于是 从左走能取到最大值还是从右走能取到最大值,只要 左右两道路径上的最大值求出来了才能作出决策。