POJ 2533 Longest Ordered Subsequence

简介: DescriptionA numeric sequence of ai is ordered if a1 < a2 < … < aN. Let the subsequence of the given numeric sequence (a1, a2, …, aN) be ...

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

用动态规划做,每次从后面对前面判断
用dd[k]表示以df[k]作为终点的最大上升子序列
则:
dd[1] = 1;
dd[k] = Max (dd[i]:1 <= i < k 且 df[i ]< df[k] 且 k != 1) + 1.
也就是第k+1前面一个不大于df[k]的数的dd[ ]的值;
n:7
i :0 1 2 3 4 5 6
df :1 7 3 5 9 4 8
dd[0]:1;
dd[1]:dd[0]+1=2;
dd[2]:dd[0]+1=2;
dd[3]:dd[2]+1=3;
dd[4]:因为df[0],df[1],df[2],df[3]都小于df[4],但是dd[3]最大,
所以,dd[4]=dd[3]+1=4;
dd[5]:dd[2]+1=3;
dd[6]:dd[5]+1=4;
…………………………………………………………

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define Maxn 1020
int df[Maxn],dd[Maxn];

int cmp(const void *x,const void *y){
    return (*(int *)y-*(int *)x);
    /*快速排序,从大到小排序*/
}
int main(){
    int n;
    while(scanf("%d",&n)==1){
        for(int i=0;i<n;i++){
            scanf("%d",&df[i]);
        }
        dd[0]=1;
        for(int i=1;i<n;i++){
                int t=0;
            for(int j=0;j<i;j++){
                if(df[i]>df[j]){
                    if(t<dd[j]){
                        t=dd[j];
                    }
                }
            }
            dd[i]=t+1;
            //此时的t是dd[i]之前的最大增子序列的个数
        }
        qsort(dd,n,sizeof(int),cmp);
        printf("%d\n",dd[0]);
    }
    return 0;
}
目录
相关文章
Leetcode 516. Longest Palindromic Subsequence
找到一个字符串的最长回文子序列,这里注意回文子串和回文序列的区别。子序列不要求连续,子串(substring)是要求连续的。leetcode 5. Longest Palindromic Substring就是求连续子串的。
58 0
|
开发框架 .NET
poj 3468 A Simple Problem with Integers线段树区间修改
题目意思很简单,有N个数,Q个操作, Q l r 表示查询从l到r 的和,C l r v 表示将从l到r 的值加上v,明显的线段树,不知道线段树的人肯定暴力,肯定超时,哈哈!!
36 0
|
算法
LeetCode 300. Longest Increasing Subsequence
给定一个无序的整数数组,找到其中最长上升子序列的长度。
58 0
LeetCode 300. Longest Increasing Subsequence
|
存储
LeetCode 329. Longest Increasing Path in a Matrix
给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
81 0
LeetCode 329. Longest Increasing Path in a Matrix
LeetCode 409. Longest Palindrome
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。 在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。
90 0
LeetCode 409. Longest Palindrome
|
人工智能
POJ 2533 Longest Ordered Subsequence
POJ 2533 Longest Ordered Subsequence
117 0
【LeetCode】Increasing Triplet Subsequence(334)
  Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.
106 0