POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

POJ_2533 Longest Ordered Subsequence【DP】【最长递增子序列】

Longest Ordered Subsequence
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 58448 Accepted: 26207

Description
A numeric sequence of ai is ordered if a1 < a2 < … < aN. Let the subsequence of the given numeric sequence (a1, a2, …, aN) be any sequence (ai1, ai2, …, aiK), where 1 <= i1 < i2 < … < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

Input
The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

Output
Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

Sample Input

7
1 7 3 5 9 4 8

Sample Output

4

题意
给出一个数组,求数组中的最长递增子序列的长度

思路一
我们可以用两个数组,第一个数组为原数组,第二个数组为原数组经过排序加去重(如果是非下降子序列就不需要去重),然后求两个数组的最长公共子序列就可以了。

AC代码一

#include <iostream>          //转化为求LCS 
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;

typedef long long LL;
const double PI  = 3.14159265358979323846264338327;
const double E   = 2.718281828459;  
const double eps = 1e-6;
const int MAXN   = 0x3f3f3f3f;
const int MINN   = 0xc0c0c0c0;
const int maxn   = 1e3 + 5; 
const int MOD    = 1e9 + 7;
int a[maxn], b[maxn], dp[maxn][maxn];

int main()
{
    int n;
    cin >> n;
    int i, j;
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        b[i] = a[i];
    }
    sort(b, b + n);
    int dre = unique(b, b + n) - b;    //需要去重  因为是最长上升的 如果是非下降,那么不需要去重 
    memset(dp, 0, sizeof(dp));
    for (i = 0; i < dre; i++)
    {
        if (a[0] == b[i])
            dp[0][i] = 1;
        else if (i)
            dp[0][i] = dp[0][i - 1];
    }
    for (i = 0; i < n; i++)
    {
        if (b[0] == a[i])
            dp[i][0] = 1;
        else if (i)
            dp[i][0] = dp[i - 1][0];
    }
    for (i = 1; i < n; i++)
    {
        for (j = 1; j < dre; j++)
        {
            if (a[i] == b[j])
                dp[i][j] = dp[i - 1][j - 1] + 1;
            else
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
    }
    cout << dp[n - 1][dre - 1] << endl;
}

思路二
如果一个数小于它前面的一个数,那么到这个数为止的最长上升子序列就是前面那个数的最长上升子序列 + 1 然后每个数往前扫一遍就可以了

AC代码二

#include <iostream>    //DP
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;

typedef long long LL;
const double PI  = 3.14159265358979323846264338327;
const double E   = 2.718281828459;  
const double eps = 1e-6;
const int MAXN   = 0x3f3f3f3f;
const int MINN   = 0xc0c0c0c0;
const int maxn   = 1e3 + 5; 
const int MOD    = 1e9 + 7;
int arr[maxn], dp[maxn];

int main()
{
    int n;
    cin >> n;
    int i, j;
    for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);
    memset(dp, 0, sizeof(dp));
    int ans = 1;
    for (i = 0; i < n; i++)
    {
        dp[i] = 1;
        for (j = i - 1; j >= 0; j--)
        {
            if (arr[i] > arr[j])
                dp[i] = max(dp[i], dp[j] + 1);
        }
        if (dp[i] > ans)
            ans = dp[i];
    }
    cout << ans << endl;
}

转载于:https://www.cnblogs.com/Dup4/p/9433379.html

“华为杯”第十八届中国研究生数学建模竞赛是一项全国性赛事,致力于提升研究生的数学建模与创新实践能力。数学建模是将实际问题转化为数学模型,并运用数学方法求解以解决实际问题的科学方法。该竞赛为参赛者提供了展示学术水平和团队协作精神的平台。 论文模板通常包含以下内容:封面需涵盖比赛名称、学校参赛队号、队员姓名以及“华为杯”和中国研究生创新实践系列大赛的标志;摘要部分应简洁明了地概括研究工作,包括研究问题、方法、主要结果和结论,使读者无需阅读全文即可了解核心内容;目录则列出各章节标题,便于读者快速查找;问题重述部分需详细重新阐述比赛中的实际问题,涵盖背景、原因及重要性;问题分析部分要深入探讨每个问题的内在联系与解决思路,分析各个子问题的特点、难点及可能的解决方案;模型假设与符号说明部分需列出合理假设以简化问题,并清晰定义模型中的变量和符号;模型建立与求解部分是核心,详细阐述将实际问题转化为数学模型的过程,以及采用的数学工具和求解步骤;结果验证与讨论部分展示模型求解结果,评估模型的有效性和局限性,并对结果进行解释;结论部分总结研究工作,强调模型的意义和对未来研究的建议;参考文献部分列出引用文献,遵循规范格式。 在准备竞赛论文时,参赛者需注重逻辑清晰、论述严谨,确保模型科学实用。良好的团队协作和时间管理也是成功的关键。通过竞赛,研究生们不仅锻炼了数学应用能力,还提升了团队合作、问题解决和科研写作能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值