题目链接:http://poj.org/problem?id=1631

这个题题目有些难看懂hhh,但实质就是求LIS--longest increasing sequence。

以下介绍LIS的解法模板:

一.O(n^2)解法

用a数组存储数据,f[i]表示以a[i] 结尾的最长子序列的长度,这样max(f[i])就是所求结果。

代码如下:

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 int n,p;
 6 int a[40005],f[40005];
 7 int res;
 8 
 9 int main(){
10     scanf("%d",&n);
11     while(n--){
12         res=1;
13         scanf("%d",&p);
14         for(int i=1;i<=p;i++)
15             scanf("%d",&a[i]),f[i]=1;
16         for(int i=2;i<=p;i++)
17             for(int j=1;j<i;j++)
18                 if(a[j]<a[i]){
19                     f[i]=max(f[i],f[j]+1);
20                     if(f[i]>res) res=f[i];
21                 }
22         printf("%d\n",res);
23     }
24     return 0;
25 }
View Code

相关文章:

  • 2021-08-19
  • 2021-10-11
  • 2021-07-28
  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
  • 2021-11-27
  • 2022-12-23
猜你喜欢
  • 2021-09-27
  • 2021-09-13
  • 2022-12-23
  • 2021-08-20
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案