/*
Longest increasing subsequence, 参考wikipedia ,时间复杂度 O(n*lgn)
*/
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 30;
int A[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};
int n = sizeof(A) / sizeof(A[0]);
int M[MAXN];
int P[MAXN];
int l = 0;
int bs(int i)
{
int x = 1, y = l+1, m;
while(x < y) {
m = x + (y-x)/2;
if(A[M[m]] >= A[i]) {
y = m;
} else {
x = m+1;
}
}
return x;
}
void print(int len, int i)
{
if(!len) return;
print(len-1, P[i]);
printf("%d ", A[i]);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int j;
for(int i=0; i<n; i++) {
j = bs(i) - 1;
P[i] = M[j];
if(j == l || A[M[j+1]] > A[i]) {
M[j+1] = i;
l = max(l, j+1);
}
}
print(l, M[l]);
}
Longest increasing subsequence,最长递增子序列
最新推荐文章于 2020-07-25 13:13:34 发布