poj 2533 Longest Ordered Subsequence

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

题目大意:求最长上升子序列的长度

解题思路:简单 dp, 时间复杂度 O(n2), 另有时间复杂度为 O(n·logn) 的算法,读者可自行思考。

 1 ///////////////////////////////////////////////////////////////////////////
 2 //problem_id: poj 2533
 3 //user_id: SCNU20102200088
 4 ///////////////////////////////////////////////////////////////////////////
 5 
 6 #include <algorithm>
 7 #include <iostream>
 8 #include <iterator>
 9 #include <iomanip>
10 #include <cstring>
11 #include <cstdlib>
12 #include <string>
13 #include <vector>
14 #include <cstdio>
15 #include <cctype>
16 #include <cmath>
17 #include <queue>
18 #include <stack>
19 #include <list>
20 #include <set>
21 #include <map>
22 using namespace std;
23 
24 ///////////////////////////////////////////////////////////////////////////
25 typedef long long LL;
26 const double PI=acos(-1.0);
27 
28 const int x4[]={-1,0,1,0};
29 const int y4[]={0,1,0,-1};
30 const int x8[]={-1,-1,0,1,1,1,0,-1};
31 const int y8[]={0,1,1,1,0,-1,-1,-1};
32 
33 typedef int T;
34 T max(T a,T b){ return a>b? a:b; }
35 T min(T a,T b){ return a<b? a:b; }
36 ///////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////
39 //Add Code:
40 ///////////////////////////////////////////////////////////////////////////
41 
42 int main(){
43     ///////////////////////////////////////////////////////////////////////
44     //Add code:
45     int n,i,j,a[1005],dp[1005];
46     scanf("%d",&n);
47     for(i=1;i<=n;i++){
48         scanf("%d",&a[i]);
49         dp[i]=1;
50     }
51     int Max=1;
52     for(i=2;i<=n;i++){
53         for(j=1;j<i;j++){
54             if(a[j]<a[i]) dp[i]=max(dp[i],dp[j]+1);
55         }
56         Max=max(Max,dp[i]);
57     }
58     printf("%d
",Max);
59     ///////////////////////////////////////////////////////////////////////
60     return 0;
61 }
62 
63 ///////////////////////////////////////////////////////////////////////////
64 /*
65 Testcase:
66 Input:
67 7
68 1 7 3 5 9 4 8
69 Output:
70 4
71 */
72 ///////////////////////////////////////////////////////////////////////////
原文地址:https://www.cnblogs.com/linqiuwei/p/3279506.html