-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_LIS.cpp
More file actions
67 lines (59 loc) · 1.06 KB
/
11_LIS.cpp
File metadata and controls
67 lines (59 loc) · 1.06 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* P432 最长不下降子序列
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 元序列
vector<int> numSq;
// 存储以某点为右端点的最长不下降子序列长度
vector<int> dp;
void init()
{
cout << "Input the number of numSq: ";
int num;
cin >> num;
for (int i = 0; i < num; i++)
{
int temp;
cin >> temp;
numSq.push_back(temp);
// 设置每个的最长长度初始为 1
dp.push_back(1);
}
}
// main function
void calLIS()
{
for (int i = 0; i < numSq.size(); i++)
{
for (int j = 0; j < i; j++)
{
if (numSq[i] >= numSq[j] && (dp[j] + 1 > dp[i]))
{
// 更新
dp[i] = dp[j] + 1;
}
}
}
}
void print()
{
int result = 0;
for (int i = 0; i < numSq.size(); i++)
{
if (dp[i] > result)
{
result = dp[i];
}
}
cout << "LIS: " << result << endl;
}
int main()
{
init();
calLIS();
print();
return 0;
}