标签:
1. LIS
O (n^2):
/* LIS(Longest Increasing Subsequence) 最长上升子序列 O (n ^ 2) 状态转移方程:dp[i] = max (dp[j]) + 1 (a[j] < a[i],1 <= j < i) 附带有print输出路径函数 */ void LIS(void) { int ret = 0, last = 0; for (int i=1; i<=n; ++i) { dp[i] = 1; fa[i] = -1; for (int j=1; j<i; ++j) { if (a[j] < a[i] && dp[i] < dp[j] + 1) { dp[i] = dp[j] + 1; fa[i] = j; } } if (ret < dp[i]) { ret = dp[i]; last = i; } } printf ("%d\n", ret); print (last); puts (""); } void print(int x) { if (fa[x] != -1) { print (fa[x]); printf (" %d", a[x]); } else printf ("%d", a[x]); }
O (nlogn):
/* LIS 二分查找优化, O(nlogn) 设当前最长递增子序列为len,考虑元素a[i]; 若d[len]<a[i],则len++,并使d[len]=a[i]; 否则,在d[1~len]中二分查找第一个大于等于a[i]的位置j,使d[j]=a[i]。附上打印路径代码(准确性未知) */ void LIS(void) { int len = 1; d[1] = a[1]; fa[1] = -1; for (int i=2; i<=n; ++i) { if (d[len] < a[i]) { d[++len] = a[i]; pos[len] = i; fa[i] = pos[len-1]; } else { int j = lower_bound (d+1, d+1+len, a[i]) - d; d[j] = a[i]; pos[j] = i; fa[i] = (j == 1) ? -1 : pos[j-1]; } } printf ("%d\n", len); vector<int> res; int i; for (i=pos[len]; ~fa[i]; i=fa[i]) res.push_back (a[i]); res.push_back (a[i]); for (int i=res.size ()-1; i>=0; --i) printf ("%d%c", res[i], i == 0 ? ‘\n‘ : ‘ ‘); }
标签:
原文地址:http://www.cnblogs.com/Running-Time/p/4778309.html