-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1008.cpp
More file actions
62 lines (54 loc) · 1.23 KB
/
Copy path1008.cpp
File metadata and controls
62 lines (54 loc) · 1.23 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
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int getans(int n, int ans1, int ans2, vector<int> &h, vector<int> &dp) {
for (int i = 1; i <= n; i++) {
dp[i] = 1; // 初始化 dp 数组
}
for (int i = 2; i <= n; i++) {
for (int j = 1; j < i; j++) {
if (h[j] >= h[i]) {
dp[i] = max(dp[i], dp[j] + 1); // 状态转移方程
}
}
}
for (int i = 1; i <= n; i++) {
ans1 = max(ans1, dp[i]); // 计算最长下降子序列的长度
}
return ans1;
}
int main() {
int m, n, ans1, ans2;
cin >> m;
while (m--) {
vector<int> h(100);
vector<int> dp(100);
cin >> n;
h.erase(h.begin() + n + 1, h.begin() + 100);
dp.erase(dp.begin() + n + 1, dp.begin() + 100);
for (int i = 1; i <= n; i++) {
cin >> h[i];
dp[i] = 1; // 初始化 dp 数组
}
ans1 = ans2 = 0;
cout << getans(n, ans1, ans2, h, dp) << " ";
int count = 0;
int test = n;
// 然后扫描dp数组,将所有长度为ans1的不上升子序列的元素顺序删除
while (h.size() > 1) {
int temp = getans(h.size() - 1, ans1, ans2, h, dp);
for (int i = test; i >= 1 && temp > 0; i--) {
if (dp[i] == temp) {
h.erase(h.begin() + i);
dp.erase(dp.begin() + i);
test--;
temp--;
}
}
count++;
}
cout << count << endl;
}
}