zoukankan      html  css  js  c++  java
  • 算法问题实战策略 JLIS 动态规划

    地址 https://algospot.com/judge/problem/read/JLIS

    先上代码,
    本题很明显的就是最长递增子序列的二维版本。
    所以我毫不犹豫的就设置了dp[i][j]
    但是在变量意义上犯了错误
    我开始设想的是变量的意义是i j代表两个数组中元素最大的那个作为结尾的递增序列长度,转移方程老想一次长度可以加2。
    事实是错误的,一次只增加长度1,分两次计算后,再来去最大值作为答案。

    #include <iostream>
    #include <algorithm>
    #include <memory.h>
    #include <limits.h>
    
    using namespace std;
    
    
    
    /*
    4
    3 3
    1 2 4
    3 4 7
    3 3
    1 2 3
    4 5 6
    5 3
    10 20 30 1 2
    10 20 30
    5 5
    2 9 9 9 1
    5 7 3 6 3
    */
    
    
    int loop;
    
    const int N = 300;
    int A[N];
    int B[N];
    
    int dp[N][N];
    int ans = INT_MIN;
    
    
    void Check(int x, int y)
    {
        
        for (int i = x; i >= 0; i--) {
            int maxLen = max(A[i], B[y]);
            if (A[x + 1] > maxLen) {
                dp[x + 1][y] = max(dp[x + 1][y], dp[i][y] + 1);
                ans = max(ans, dp[x + 1][y]);
            }
        }
    
        for (int i = y; i >= 0; i--) {
            int maxLen = max(A[x], B[i]);
            if (B[y + 1] > maxLen) {
                dp[x][y + 1] = max(dp[x][y + 1], dp[x][i] + 1);
                ans = max(ans, dp[x][y+1]);
            }
        }
    
    }
    
    int main()
    {
        cin >> loop;
    
        while (loop--) {
            int Alen, Blen;
            cin >> Alen >> Blen;
            memset(A, 0, sizeof A); memset(B, 0, sizeof B);
            memset(dp, 0, sizeof(dp)); ans = INT_MIN;
            A[0] = B[0] = INT_MIN;
            for (int i = 1; i <= Alen; i++) { cin >> A[i]; }
            for (int i = 1; i <= Blen; i++) { cin >> B[i]; }
    
            for (int i = 0; i <= Alen; i++) {
                for (int j = 0; j <= Blen; j++) {
                    Check(i, j);
                }
            }
            cout << ans << endl;
        }
    
    
        return 0;
    }
    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    数据库自动备份
    VC查找文件特定位置的记录方法
    MFC利用ADO连接ACCESS数据库及其操作数据库的方法
    VC利用ODBC连接MySql数据库的方法及其操作数据的方法
    uwsgi和nginx的故事
    A JavaScript Image Gallery
    The DOM in JavaScript
    A brief look at the Objects in JavaScript
    3 ways of including JavaScript in HTML
    #3 working with data stored in files && securing your application (PART II)
  • 原文地址:https://www.cnblogs.com/itdef/p/13730695.html
Copyright © 2011-2022 走看看