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;
}

POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】的更多相关文章

  1. POJ2533 Longest Ordered Subsequence —— DP 最长上升子序列(LIS)

    题目链接:http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 6 ...

  2. 【POJ - 2533】Longest Ordered Subsequence (最长上升子序列 简单dp)

    Longest Ordered Subsequence 搬中文 Descriptions: 给出一个序列,求出这个序列的最长上升子序列. 序列A的上升子序列B定义如下: B为A的子序列 B为严格递增序 ...

  3. 题解报告:poj 2533 Longest Ordered Subsequence(最长上升子序列LIS)

    Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence ...

  4. POJ 2533 Longest Ordered Subsequence(最长上升子序列(NlogN)

    传送门 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subseque ...

  5. POJ2533 Longest Ordered Subsequence 【最长递增子序列】

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32192   Acc ...

  6. POJ - 2533 Longest Ordered Subsequence(最长上升子序列)

    d.最长上升子序列 s.注意是严格递增 c.O(nlogn) #include<iostream> #include<stdio.h> using namespace std; ...

  7. [POJ2533]Longest Ordered Subsequence<dp>

    题目链接:http://poj.org/problem?id=2533 描述: A numeric sequence of ai is ordered if a1 < a2 < ... & ...

  8. POJ_2533 Longest Ordered Subsequence 【LIS】

    一.题目 Longest Ordered Subsequence 二.分析 动态规划里的经典问题.重在DP思维. 如果用最原始的DP思想做,状态转移方程为$DP[i] = max(DP[j] + 1) ...

  9. poj 2533 Longest Ordered Subsequence(dp)

    题目:http://poj.org/problem?id=2533 题意:最长上升子序列.... 以前做过,课本上的思想 #include<iostream> #include<cs ...

随机推荐

  1. MYSQL数据库连接

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  2. js json 单双引号互换

    var _adrobj = JSON.stringify(address).replace(/\"/g,"'"); var _nstr = _adrdata.replac ...

  3. PostgreSql Partition + Hibernate Insert

    与Oracle不同.PostgreSQL须要手动控制分区规则触发器. 步骤一:创建分区 CREATE TABLE table_partition_1( CHECK partition_column c ...

  4. Http服务器实现文件上传与下载(四)

    一.引言 欢迎大家来到和我一起编写Http服务器实现文件的上传和下载,现在我稍微回顾一下之前我说的,第一.二章说明说明了整体的HTTP走向,第三章实现底层的网络编程.接着这一章我想给大家讲的是请求获取 ...

  5. bzoj 1415(概率dp和bfs预处理)

    感觉挺经典的一道题目. 先用 bfs 预处理下一步走到的位置.因为每一步走法都是固定的,所以可以用dp的方法来做. 1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec  M ...

  6. JZOJ.5235【NOIP2017模拟8.7】好的排列

    Description 对于一个1->n的排列 ,定义A中的一个位置i是好的,当且仅当Ai-1>Ai 或者Ai+1>Ai.对于一个排列A,假如有不少于k个位置是好的,那么称A是一个好 ...

  7. JZOJ.3777【NOI2015模拟8.17】最短路(shortest)

    Description        小Y最近学得了最短路算法,一直想找个机会好好练习一下.话虽这么说,OJ上最短路的题目都被他刷光了.正巧他的好朋友小A正在研究一类奇怪的图,他也想凑上去求下它的最短 ...

  8. java的奇技淫巧--意外行为与特性(译文)

    Java是一种非常成熟的编程语言 - 事实上,它已经走过21年了,如果它是一个人,它可以在美国随便混!随着年龄的增长,智慧也在增长,而至少有时候,有些东西会变得很怪异.在本文中,我将介绍Java语言的 ...

  9. 170213、亿级Web系统搭建——单机到分布式集群

    [导读]徐汉彬曾在阿里巴巴和腾讯从事4年多的技术研发工作,负责过日请求量过亿的Web系统升级与重构,目前在小满科技创业,从事SaaS服务技术建设. 大规模流量的网站架构,从来都是慢慢“成长”而来.而这 ...

  10. CoordinatorLayout Behaviors使用说明[翻译]

    翻译与:Intercepting everything with CoordinatorLayout Behaviors 使用过Android Design Support Library的小伙伴应该 ...