最長上升子序列
1、\(O(n^{2})\) 簡單DP做法
#include<bits/stdc++.h>
using namespace std;
const int N = 105;
int h[N];
int dp[N];
int main() {
int T; cin >> T;
while(T --) {
int n; cin >> n;
for(int i = 1; i <= n; ++ i) cin >> h[i];
memset(dp, 0, sizeof(dp));
int res = 0;
for(int i = 1; i <= n; ++ i) {
for(int j = 1; j < i; ++ j) {
if(h[i] > h[j]) dp[i] = max(dp[i], dp[j] + 1);
}
res = max(res, dp[i]);
}
memset(dp, 0 ,sizeof(dp));
for(int i = 1; i <= n; ++ i) {
for(int j = 1; j < i; ++ j) {
if(h[i] < h[j]) dp[i] = max(dp[i], dp[j] + 1);
}
res = max(res, dp[i]);
}
cout << res + 1 << endl;
}
return 0;
}
2、\(O(nlogn)\)的二分+貪心法
簡單來說:就是q[i]表示所有序列爲i的上升序列的最小末尾元素, 如[1,2,3],[5,4,2]這種長度爲3的序列,那麼q[3]=2。這樣處理到第i位數字前,q數組就維護了[1~i-1]所有序列的最小最後一個元素,那麼想想看能把h[i]加在哪個位置,那麼dp[i]=該位置編號。 至於這個位置就是第一個大於等於h[i]的位置。
#include<bits/stdc++.h>
using namespace std;
const int N = 105;
int h[N];
int q[N];
int main() {
int T; cin >> T;
while(T --) {
int n; cin >> n;
for(int i = 1; i <= n; ++ i) cin >> h[i];
memset(q, 0x3f3f3f3f, sizeof(q));
int res = 0;
for(int i = 1; i <= n; ++ i) {
int x = h[i];
int tx = lower_bound(q + 1, q + n + 1, x) - q;
q[tx] = x;
res = max(res, tx);
}
memset(q, 0x3f3f3f3f, sizeof(q));
for(int i = n; i >= 1; --i) {
int x = h[i];
int tx = lower_bound(q + 1, q + n + 1, x) - q;
q[tx] = x;
res = max(res, tx);
}
cout << res << endl;
}
return 0;
}
3、\(O(nlogn)\) 樹狀數組優化
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int h[N];
int q[N];
int c[N];
// 樹狀數組維護區間最大值模板
int lowbit(int x){ return x & -x;}
int ask(int x) {
int ans = 0;
for(; x; x -= lowbit(x)) ans = max(ans, c[x]);
return ans;
}
void add(int x, int y) {
for(; x < N; x += lowbit(x)) c[x] = max(c[x], y);
}
int main() {
int T; cin >> T;
while(T --) {
int n; cin >> n;
for(int i = 1; i <= n; ++ i) cin >> h[i];
int res = 0;
memset(c, 0, sizeof(c));
for(int i = 1; i <= n; ++ i) {
add(h[i], ask(h[i] - 1) + 1);
}
res = max(res, ask(N - 1));
memset(c, 0, sizeof(c));
for(int i = n; i >= 1; -- i) {
add(h[i], ask(h[i] - 1) + 1);
}
res = max(res, ask(N - 1));
cout << res << endl;
}
return 0;
}