您的位置:首页 > 其它

动态规划 — LIS

2017-10-29 12:08 176 查看

动态规划介绍

动态规划是比较重要的算法啦,打算慢慢写几篇博客总结一下。

动态规划的基本思想是:要求原问题的解,先求出更小一些的问题的解,由子问题的解通过某种方式得到原问题的解。

通常,我们也称每个子问题是一个状态,由子问题计算原问题,称为状态迁移

由子问题计算原问题的方式称为状态转移方程

一个动态规划算法的复杂度通常取决于状态的数量和状态迁移的复杂度

以上是有关动态规划的一些基本概念和概括。

LIS问题

第一篇就先讲一下最长递增子序列(LIS)的问题的解决。

给出一个数列A,求A的一个 长度最大的子数列B,使得B是一个递增数列

Example:

数列A: 5, 2, 8, 6, 3, 6, 9, 7

一个递增的子数列: 5, 8, 9

一个长度最大的递增子数列:2, 3, 6, 9

思路

将每一个数作为一个结点。从结点u到结点v连一条边,当且仅当u代表的数在原数列中出现在v代表的数前面,而且u代表的数比v代表的数小。这样转化保证了dag(有向无环图)的路径和原数列的递增子序列一一对应,因而dag上的最长路就是原数列的最长递增子序列。

代码

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
int size, num;
cin >> size;
vector<int> nums;
for (int i = 0; i < size; i++) {
cin >> num;
nums.push_back(num);
}

vector<int> result;
result.assign(nums.size(), 1);
for (int i = 1; i < nums.size(); i++) {
int max = 0;
for (int j = 0; j < i; j++) {
if (result[j] > max && nums[j] < nums[i]) {
max = result[j];
}
}
result[i] = 1 + max;
}

int max = 0;
for (int i = 0; i < result.size(); i++) {
if (max < result[i])
max = result[i];
}

cout << max << endl;
}


* 有n个状态,每个状态迁移的复杂度是O(n),因而总复杂度为O(n^2)。*

改进

我们可以看到,在生成递增子序列中,有一些值是不需要维护,可以直接去掉的,这样做可以减少状态迁移的复杂度。

考虑这样一个事情,遍历数组的时候,比如,例子中所给的:

5, 2, 8, 6, 3, 6, 9, 7

第一个数5,直到当前,最长递增序列有1个数。

第二个数2,直到当前,最长递增序列还是只有1个数。

此时,无论后面的数是什么,只需要这个数比2大,那么就能将最长递增序列增加到两个数。而5已经没有价值了。因为一个数若大于5必定大于2,而且与2相比,更有可能获取更长的递增序列。

简单来说,就是维护一个结果数组,数组表示结果需要的递增数列的每一个值,数组的长度就是最长递增序列的长度。(当然,结果不唯一)

vector<int> result;
result.push_back(nums[0]);
for (int i = 1; i < nums.size(); i++) {
int index = binary_search(0, result.size() - 1, result, nums[i]);
//if we the new num is the largest num in the result vector
if (index == result.size() || result[index] < nums[i]) {
result.push_back(nums[i]);
} else if (result[index] > nums[i]) {
result[index] = nums[i];
}
}


通过二分查找,找到遍历到的数在结果数组中的位置(即找到第一个比该数大的数在结果数组中的下标),如果该位置不存在,表示比结果数组中所有的数都大,那么插入该数到结果数组的尾部。否则,判断该数是否比该位置的数小,如果是,替换之。

代码

#include <iostream>
#include <vector>

using namespace std;

//find the index that the num should be inserted
int binary_search(int low, int high, vector<int>& nums, int num) {
while (low <= high) {
int mid = (low + high) / 2;
if (nums[mid] == num) return mid;
if (nums[mid] < num)
low = mid + 1;
else
high = mid - 1;
}
return low;
}

int binary_search_recursion(int low, int high, vector<int>& nums, int num) {
int mid = (low + high) / 2;
if (low > high) return low;
if (nums[mid] == num) return mid;
if (nums[mid] < num) {
return binary_search_recursion(mid + 1, high, nums, num);
} else {
return binary_search_recursion(low, mid - 1, nums, num);
}
}

int main() {
int size, num;
cin >> size;
vector<int> nums;
for (int i = 0; i < size; i++) {
cin >> num;
nums.push_back(num);
}

vector<int> result; result.push_back(nums[0]); for (int i = 1; i < nums.size(); i++) { int index = binary_search(0, result.size() - 1, result, nums[i]); //if we the new num is the largest num in the result vector if (index == result.size() || result[index] < nums[i]) { result.push_back(nums[i]); } else if (result[index] > nums[i]) { result[index] = nums[i]; } }

cout << result.size() << endl;
}


可以看到n次遍历,每次遍历二分查找的复杂度为O(log n),总的复杂度是O(n*log n)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: