POJ 2757: Longest ascending subsequence (JAVA)

Source: Internet
Author: User

Total time limit:
2000ms
Memory limit:
65536kB
Describe
A number of sequence bi, when B1 < B2 < ... < BS , we call this sequence ascending.  For a given sequence ( A1, A2, ..., an), We can get some ascending sub-sequences ( ai1, ai2, ..., AiK), here 1 <= i1 < i2 < ... < IK <= N. For example, for sequences (1, 7, 3, 5, 9, 4, 8), there are some ascending sub-sequences of it, such as (1, 7), (3, 4, 8) and so on. The longest length of these subsequence sequences is 4, such as subsequence (1, 3, 5, 8).

Your task is to find the length of the longest ascending sub-sequence for a given sequence.
Input
The first line of input is the length of the sequence n (1 <= n <= 1000). The second line gives the n integers in the sequence, with values ranging from 0 to 10000.
Output
The length of the longest ascending sub-sequence.
Sample input
71 7 3 5 9 4 8
Sample output
4
Source
translated from Northeastern Europe 2002, Far-eastern subregion's contest questions
Importjava.util.ArrayList;Importjava.util.Arrays;ImportJava.util.Scanner; Public classmain{ Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intn = in.nextint ();//sequence length 1-1000        int[] L =New int[N],a =New int[n];//The length of the oldest sequence with a particular node in focus                 for(intI =0;i<n;i++) {A[i]= In.nextint ();//Enter the number of I, and the longest ascending subsequence of the end is calculated below            if(i==0) L[i] = 1; Else            {                intK = 0,longest = 0;  for(intj=0;j<i;j++)                {                    if(A[j]<a[i])//The number in front of the array is smaller than the number                    {                        if(k = = 0) longest = l[j];//The first one is smaller than him .                        Else                        {                            if(L[j] > Longest)//find the longestLongest =L[j]; } k++; }//ifL[i] = longest+1; }// for}//Else} arrays.sort (L); System.out.println (L[l.length-1]); }}

Summary:

This POJ problem are 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 was shameful to admit, I found a only too brilliant idea which was to caculate the Longg EST 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 is less than itself:

Longest = 0



POJ 2757: Longest ascending subsequence (JAVA)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.