标签:
7 1 7 3 5 9 4 8
4
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
标签:
原文地址:http://www.cnblogs.com/danscarlett/p/5656464.html