Files
python/TangDou/AcWing/LIS/1017_PuSu.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

40 lines
1.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n; // 楼房的个数
int w[N]; // 楼房的高度数组
int f[N]; // LIS结果数组,DP结果
int main() {
int T;
cin >> T;
while (T--) {
// 保持好习惯,多组测试数据,记得每次清空结果数组
memset(f, 0, sizeof f);
int res = 0;
cin >> n;
for (int i = 1; i <= n; i++) cin >> w[i];
// 从左到右,求一遍最长上升子序列[朴素O(N^2)版本]
for (int i = 1; i <= n; i++) {
f[i] = 1;
for (int j = 1; j < i; j++)
if (w[i] > w[j]) f[i] = max(f[i], f[j] + 1);
res = max(res, f[i]);
}
// 反向求解 LIS问题
for (int i = n; i >= 1; i--) {
f[i] = 1;
for (int j = n; j > i; j--)
if (w[i] > w[j]) f[i] = max(f[i], f[j] + 1);
res = max(res, f[i]);
}
printf("%d\n", res);
}
return 0;
}