PTA7-6最长上升子序列

一、题目描述

 

 二、解题思路

  简单dp,dp数组的含义为前i个数的最长上升子序列,因为这里的数据范围很小,故不需要二分进行优化,最后扫一遍整个数组的最大值就是答案。

三、代码实现

 1 #include "bits/stdc++.h"
 2 #define PII pair<int,int>
 3 #define rep(i,z,n) for(int i = z;i <= n; i++)
 4 #define per(i,n,z) for(int i = n;i >= z; i--)
 5 #define ll long long
 6 #define db double
 7 #define vi vector<int>
 8 #define debug(x) cerr << "!!!" << x << endl;
 9 using namespace std;
10 inline ll read()
11 {
12     ll s,r;
13     r = 1;
14     s = 0;
15     char ch = getchar();
16     while(ch < '0' || ch > '9'){
17         if(ch == '-')
18             r = -1;
19         ch = getchar();
20     }
21     while(ch >= '0' && ch <= '9'){
22         s = (s << 1) + (s << 3) + (ch ^ 48);
23         ch = getchar();
24     }
25     return s * r;
26 }
27 inline void write(ll x)
28 {
29     if(x < 0) putchar('-'),x = -x;
30     if(x > 9) write(x / 10);
31     putchar(x % 10 + '0');
32 }
33 int dp[1100];
34 int a[1100];
35 int main()
36 {
37     int n;
38     n = read();
39     for(int i = 1;i <= n;i++){
40         cin >> a[i];
41         dp[i] = 1;
42     }
43     for(int i = 2;i <= n;i++)
44         for(int j = 1;j < i;j++)
45             if(a[i] > a[j])
46                 dp[i] = max(dp[i],dp[j] + 1);
47     int mm = -INT_MAX;
48     for(int i = 1;i <= n;i++)
49         mm = max(dp[i],mm);
50     cout << mm;
51     return 0;
52 }

 

posted @ 2022-03-10 21:20  scannerkk  阅读(112)  评论(0)    收藏  举报