-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1009.cpp
More file actions
43 lines (43 loc) · 973 Bytes
/
Copy path1009.cpp
File metadata and controls
43 lines (43 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <algorithm>
using namespace std;
// 这题的本质就是找一个最长不增子序列,用动态规划解决
int main()
{
int m;
cin >> m;
for (int i = 0;i < m;i++)
{
int n;
cin >> n;
int temp[100];
int dp[100] = {};
// dp[i]表示以第i个数结尾的最大不增子序列的长度
for (int j = 0;j < n;j++)
{
cin >> temp[j];
}
dp[0] = 1;
for (int j = 1;j < n;j++)
{
dp[j] = 1;
for (int k = j - 1;k >= 0;k--)
{
// 尝试把第j个数接在前面的不增子序列上,找到这个最大值
if (temp[j] <= temp[k])
{
dp[j] = max(dp[k] + 1, dp[j]);
}
}
}
int max = 0;
for (int i = 0;i < n;i++)
{
if (dp[i] >= max)
{
max = dp[i];
}
}
cout << max << endl;
}
}