时间:2016-05-11 14:16:50 星期三
题目编号:[2016-05-11][51nod][1134 最长递增子序列]
题目大意:给出长度为N的数组,找出这个数组的最长递增子序列。(递增子序列是指,子序列的元素是递增的)
分析:
- 维护一个栈,如果是更大值,加入栈顶,否则,替换栈内第一个不小于它的数字
#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int maxn = 1E4 * 5 + 10;int a[maxn],stk[maxn];int main(){ int n; scanf("%d",&n); for(int i = 1 ; i <= n ; ++i){ scanf("%d",&a[i]); } int pcur = 0; for(int i = 1;i <= n;++i){ if(!pcur){ stk[pcur++] = a[i]; }else { if(a[i] > stk[pcur - 1]){ stk[pcur++] = a[i]; }else if(a[i] < stk[pcur - 1]){ int pos = lower_bound(stk,stk + pcur,a[i]) - stk; stk[pos] = a[i]; } } } printf("%d\n",pcur); return 0;}