//最长上升子序列最快算法,非动态规划,运用了二分思想,还有栈的思想, //用每一个数去和栈中的栈顶元素相比较,如果大于栈顶元素,则入栈,否则运用二分查找,寻找出第一个比这个数大的那个数替换 #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cmath> using namespace std; int main() { int temp, tail; int n; int stack[1001]; scanf("%d", &n); tail = 0; stack[0] = -1; for (int i = 1; i <= n; i++) { scanf("%d", &temp); if (temp > stack[tail]) stack[++tail] = temp;//入栈 else { int l = 1, r = tail; int mid; while (l <= r)//二分 { mid = (l + r) / 2; if (temp > stack[mid]) l = mid + 1; else r = mid - 1; } stack[l] = temp; } } printf("%d", tail); return 0; }
时间: 2024-10-13 18:43:38