注意特殊情况!最长上升子序列!!poj2533

poj 2533

简单的动归。用O(n^2)的算法也能过。但是有个细节!刚开始ans初始化为0时是错的!!!要初始化为1。因为只有1个数的时候,下面的循环是不会执行的。。。。。或者特判。。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int MAXN = 1005;
 7 int dp[MAXN], a[MAXN];
 8 
 9 int main()
10 {
11     int N;
12     while (scanf("%d", &N) == 1)
13     {
14         for (int i = 1; i <= N; i++) {
15             scanf("%d", &a[i]);
16             dp[i] = 1;
17         }
18         int ans = 1;//初始化为1!!!N==1下面循环不会执行。
19         for (int i = 2; i <= N; i++)
20         {
21             for (int j = 1; j<i; j++) {
22                 if (a[i]>a[j])
23                     dp[i] = max(dp[i], dp[j] + 1);
24             }
25             ans = max(ans, dp[i]);
26         }
27         //if (N==1)
28           //  ans=1;
29         printf("%d
", ans);
30     }
31     return 0;
32 }
原文地址:https://www.cnblogs.com/zxhyxiao/p/7419667.html