HDU1087(最长上升子序列DP)
2015-01-09 12:01
239 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087
解题思路:
i < j 时 , dp[ i ] = max( dp[ j ] + a[ i ] , a[ i ] ) 。记录保存最大的dp[ i ] 值即可。dp[ i ] 代表从1到 i 的最大上升子序列的和,里层循环为dp[ i ]做更新,寻找1到 i 区间内的最大上升子序列和,从而使得其具有局部最优子结构。
完整代码:
解题思路:
i < j 时 , dp[ i ] = max( dp[ j ] + a[ i ] , a[ i ] ) 。记录保存最大的dp[ i ] 值即可。dp[ i ] 代表从1到 i 的最大上升子序列的和,里层循环为dp[ i ]做更新,寻找1到 i 区间内的最大上升子序列和,从而使得其具有局部最优子结构。
完整代码:
#include <functional> #include <algorithm> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <numeric> #include <cstring> #include <climits> #include <cassert> #include <complex> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <list> #include <set> #include <map> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") typedef long long LL; typedef double DB; typedef unsigned uint; typedef unsigned long long uLL; /** Constant List .. **/ //{ const int MOD = int(1e9)+7; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f3f3f3f3f3f3fLL; const DB EPS = 1e-9; const DB OO = 1e20; const DB PI = acos(-1.0); //M_PI; const int maxn = 10001; int a[maxn]; int dp[maxn]; int main() { #ifdef DoubleQ freopen("in.txt","r",stdin); #endif std::ios::sync_with_stdio(false); std::cin.tie(0); int n; while(cin >> n) { if(n == 0) break; for(int i = 0 ; i < n ; i ++) cin >> a[i]; int maxx = -INF; for(int i = 0 ; i < n ; i ++) { dp[i] = a[i]; if(dp[i] > maxx) maxx = dp[i]; for(int j = 0 ; j < i ; j ++) { if(a[i] > a[j] && dp[j] + a[i] > dp[i]) { dp[i] = dp[j] + a[i]; if(dp[i] > maxx) maxx = dp[i]; } } } cout << maxx << endl; } }
相关文章推荐
- HDU1087 Super Jumping! Jumping! Jumping!【最长上升子序列+DP】
- LIS(最长上升子序列两种算法模板)DP模板,并且输出序列
- HDU 1025 Constructing Roads In JGShining's Kingdom (简单dp, 最长上升子序列LIS)
- DP实例之最长上升子序列LIS
- hdu 1423 Greatest Common Increasing Subsequence(最长公共上升子序列dp)
- hdu 1160 dp (二维最长上升子序列 记录路径
- (hdu step 3.2.3)Super Jumping! Jumping! Jumping!(DP:求最长上升子序列的最大和)
- 【openjudge 1759】最长上升子序列(dp)
- 最长公共上升子序列的DP解法及其优化
- hdu1257 dp(最长上升子序列)
- poj1631 dp 最长上升子序列LIS
- 【序列型DP】最长严格上升子序列
- POJ3903 Stock Exchange 最长上升子序列DP]
- [NYIST16]矩形嵌套(DP,最长上升子序列)
- 【DP】HDU 1025 Constructing Roads In JGShining's Kingdom 最长上升子序列
- DP之最长上升子序列O(n*logn)算法
- 算法练习--- DP 求解最长上升子序列(LIS)
- 【基础练习】【线性DP】codevs1576 最长严格上升子序列题解
- hdu 1160 dp (二维最长上升子序列 记录路径
- 【DP经典】noi openjudge 2.6 最长上升子序列