POJ 2533 Longest Ordered Subsequence

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;
}
时间: 2024-09-30 20:36:48

POJ 2533 Longest Ordered Subsequence的相关文章

UVa 10405:Longest Common Subsequence,最长公共子序列模板题

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1346 [原题] Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of

UVa 10405 Longest Common Subsequence (DP&amp;amp;LCS)

10405 - Longest Common Subsequence Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1346 Sequence 1: Sequence 2: Given two sequences of characters, print

[算法]-Longest Increasing Subsequence

看微软笔试题遇到的. Longest Increasing Subsequence(LIS) means a sequence containing some elements in another sequence by the same order, and the values of elements keeps increasing.For example, LIS of {2,1,4,2,3,7,4,6} is {1,2,3,4,6}, and its LIS length is 5.

LCS (Longest Common Subsequence) 字符串最长公共子串算法

LCS (Longest Common Subsequence) 算法用于找出两个字符串最长公共子串. 算法原理: (1) 将两个字符串分别以行和列组成矩阵.(2) 计算每个节点行列字符是否相同,如相同则为 1.(3) 通过找出值为 1 的最长对角线即可得到最长公共子串. 人 民 共 和 时 代中 0, 0, 0, 0, 0, 0华 0, 0, 0, 0, 0, 0人 1, 0, 0, 0, 0, 0民 0, 1, 0, 0, 0, 0共 0, 0, 1, 0, 0, 0和 0, 0, 0, 1

[LeetCode] Longest Increasing Subsequence

Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than

[LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] Outpu

UVA 10405 Longest Common Subsequence:简单DP

省赛还有不到50天了,自己DP这块实在是弱,准备就拿着些天狂刷DP了. http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1346 大意: 求两个字符串的最长公共子序列. 思路:水题,不过需要注意的就是字符串里可能会出现空格,需要用gets,真是坑. 更多精彩内容:http://www.bianceng.cnhttp://www.biancen

uva 10405 Longest Common Subsequence

#include<iostream> #include<cstdio> #include<cstring> using namespace std; char str1[1002],str2[1002]; int d[1002][1002]; int main() { while(gets(str1) && gets(str2)) { int len1=strlen(str1),len2=strlen(str2); memset(d,0,sizeof(d

[LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列

Given an unsorted array of integers, find the length of longest continuous increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. Even though [1,3,5,7] i