poj 2757 : 最长上升子序列(JAVA)

总时间限制: 
2000ms

内存限制:

65536kB
描述
一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的。对于给定的一个序列(a1a2, ..., aN),我们可以得到一些上升的子序列(ai1ai2, ..., aiK),这里1 <= i1 < i2 < ... < iK <= N。比如,对于序列(1, 7, 3, 5, 9, 4, 8),有它的一些上升子序列,如(1, 7), (3, 4, 8)等等。这些子序列中最长的长度是4,比如子序列(1, 3, 5, 8).

你的任务,就是对于给定的序列,求出最长上升子序列的长度。

输入
输入的第一行是序列的长度N (1 <= N <= 1000)。第二行给出序列中的N个整数,这些整数的取值范围都在0到10000。
输出
最长上升子序列的长度。
样例输入
7
1 7 3 5 9 4 8
样例输出
4
来源
翻译自 Northeastern Europe 2002, Far-Eastern Subregion 的比赛试题
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();//序列长度 1-1000
        int[] l = new int[n],a = new int[n]; //以某一节点为重点的最长子序列的长度

        for(int i =0;i<n;i++)
        {
            a[i] = in.nextInt();//输入第i个数,下面计算它为终点的最长上升子序列
            if(i==0)    l[i] = 1;
            else
            {
                int k = 0,longest = 0;
                for(int j=0;j<i;j++)
                {
                    if(a[j]<a[i])//比该数小的数组前面的数里
                    {
                        if(k == 0) longest = l[j];//第一个比他小
                        else
                        {
                            if(l[j] > longest)//找到最长的
                                longest = l[j];
                        }
                        k++;
                    }//if
                    l[i] = longest+1;
                }//for
            }//else
        }
        Arrays.sort(l);
        System.out.println(l[l.length-1]);
    }
}

Summary:

This poj problem is another typical example for dynamic planning.Well, the idea is very delicate. I tried to calculate the longgest sbsequences before a certain number, but it couldn‘ make it out.

Then I referred to the book, which is shameful to admit, I found an only too brilliant idea which is to caculate the longgest rising subsequences‘s lengths of one ended with a certain number.

As always, I got WA first time the first time I submitted my answer. Oh no..

The reason for the failure lies in this sentance when initializing the value:

I wrote "longest=1"..which triggers to a fault when the number happens to find no one before it can be less than itself:

longest = 0
时间: 2024-08-28 17:37:21

poj 2757 : 最长上升子序列(JAVA)的相关文章

POJ 1631(最长上升子序列 nlogn).

~~~~ 由题意可知,因为左边是按1~n的顺序递增排列,要想得到不相交组合,左边后面的一定与相应右边后面的相连,如此一来, 就可以发现其实是一道最长上升子序列的题目,要注意的是N<40000,用n^2的算法一定会超时. 题目链接:http://poj.org/problem?id=1631 ~~~~ nlogn的算法在这里补充一下. 最长不下降子序列的O(nlogn)算法分析如下: 设 A[t]表示序列中的第t个数,F[t]表示从1到t这一段中以t结尾的最长上升子序列的长度,初始时设F [t]

poj 3903 &amp; poj 2533 最长上升子序列(LIS)

最长上升子序列. 做这道题之前先做了2533,再看这道题,感觉两道题就一模一样,于是用2533的代码直接交, TLE了: 回头一看,数据范围.2533 N:0~1000:3903 N :1~100000. 原因终归于算法时间复杂度. 也借这道题学习了nlgn的最长上升子序列.(学习链接:http://blog.csdn.net/dangwenliang/article/details/5728363) 下面简单介绍n^2 和 nlgn 的两种算法. n^2: 主要思想:DP: 假设A1,A2..

百练 2757:最长上升子序列

解题思路:给出一列数{an},求其最长递增子序列的长度max从特殊到一般来做,当给出的数列{an}是单调递增数列时,max=n;所以当{an}乱序时,max<=n;则我们要求max的值,只需要求{an}和按照升序排好顺序的{a'n}的最长公共子序列 2757:最长上升子序列 描述一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对于给定的一个序列(a1, a2, ..., aN),我们可以得到一些上升的子序列(ai1, ai2, ..., ai

poj之最长公共子序列

题目:poj 1458   Common Subsequence Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequenc

poj之最长递增子序列

题目: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 &l

OpenJudge Bailian 2757 最长上升子序列 DP

最长上升子序列 Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u OpenJ_Bailian 2757 Description 一个数的序列 bi,当 b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对于给定的一个序列( a1, a2, ..., aN),我们可以得到一些上升的子序列( ai1, ai2, ..., aiK),这里1 <= i1 <

[kuangbin带你飞]专题十二 基础DP1 N - Longest Ordered Subsequence POJ - 2533(最长上升子序列LIS)

N - Longest Ordered Subsequence POJ - 2533 题目链接:https://vjudge.net/contest/68966#problem/N 题目: 最长有序子序列如果a1 <a2 <... <aN,则排序ai的数字序列. 让给定数字序列(a1,a2,...,aN)的子序列为任何序列(ai1,ai2,...,aiK),其中1 <= i1 <i2 <... <iK <= N 例如,序列(1,7,3,5,9,4,8)具有有

POJ 1458 最长公共子序列 LCS

经典的最长公共子序列问题. 状态转移方程为 : if(x[i] == Y[j]) dp[i, j] = dp[i - 1, j - 1] +1 else dp[i, j] = max(dp[i - 1], j, dp[i, j - 1]); 设有字符串X和字符串Y,dp[i, j]表示的是X的前i个字符与Y的前j个字符的最长公共子序列长度. 如果X[i] == Y[j] ,那么这个字符与之前的LCS 一定可以构成一个新的LCS: 如果X[i] != Y[j] ,则分别考察 dp[i  -1][j

POJ 2250(最长公共子序列 变形)

Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germa