最长上升子序列 CSU

名词解释:

一串数字比如1、5、3、6、9、8、10,它的子序列是从左到右不连续的若干个数,比如1、5、6,3、9、8、10都是它的子序列。

最长上升子序列即从左到右严格增长的最长的一个子序列,1、5、6、9、10就是这个序列的一个最长上升子序列。

给出若干序列,求出每个序列的最长上升子序列长度。

Input

  多组数据,每组第一行正整数n,1 <= n <= 1000,第二行n个空格隔开的不大于1,000,000的正整数。

Output

 每组数据输出一行,最长上升子序列的长度。

 

Sample Input

7
1 5 3 6 9 8 10

Sample Output

5
还没有理解LIS的精髓 所以这道题是排序+LCS做的
 1 #include <iostream>
 2 using namespace std;
 3 #include<string.h>
 4 #include<set>
 5 #include<stdio.h>
 6 #include<math.h>
 7 #include<queue>
 8 #include<map>
 9 #include<algorithm>
10 #include<cstdio>
11 #include<cmath>
12 #include<cstring>
13 #include <cstdio>
14 #include <cstdlib>
15 #include<vector>
16 int a[1010],b[1010];
17 int c[1010][1010];
18 int main()
19 {
20     int t;
21     int lena=0;
22     while(cin>>t)
23     {
24         a[0]=0;
25         b[0]=0;
26         memset(c,0,sizeof(c));
27         for(int i=1;i<=t;i++)
28         {
29             lena=i;
30             cin>>a[i];
31             b[i]=a[i];
32         }
33         sort(b,b+lena+1);
34         int max1=0;
35         for(int i=1;i<=lena;i++)
36         {
37             for(int j=1;j<=lena;j++)
38             {
39                 if(a[i]==b[j])
40                     {
41                         c[i][j]=c[i-1][j-1]+1;
42                     max1=max(max1,c[i][j]);
43                     }
44                     else
45                 c[i][j]=max(c[i-1][j],c[i][j-1]);
46             }
47         }
48         cout<<max1<<endl;
49     }
50     return 0;
51 }
View Code
原文地址:https://www.cnblogs.com/dulute/p/7277763.html