题意:LIS裸题
关于最长上升子序列有段时间看了很多解法,关键是要选择较小的那个插入序列中(如1423长度为2的有14 12 这个时候应选择12因为2比4小这样后面才有可能最长)
当新加入元素时,若大于末端元素,则直接加在后面;若小于,则在序列中找到第一个不小于它的数字尝试更新,维护子序列的最优。
LIS的代码网上有很多,但是有一次看到某场比赛师兄给的题解以后觉得网上写的都好长啊。。(这是单调栈咩?
->强行更新的地方想了好久。。刚开始以为是错的,因为觉得后面插进来的怎么能把前面的更新掉呢??其实更新掉并没关系。。反正数量又不变,这里是为了,要更新末端的数值,即前面讲的要使它最小。
/*poj-3903 LIS问题
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<vector>
#define INF 100000000
#define MAXNUM 100007
#define pi 3.1415926
using namespace std;
//char num[MAXNUM];
int temp[MAXNUM];
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
memset(temp,-1,sizeof(temp));
int x;
int count=0;
for(int i=0;i<n;i++){
scanf("%d",&x);
if(x>temp[count-1]||count==0)
temp[count++]=x;
else {
int k=lower_bound(temp,temp+count,x)-temp;
temp[k]=x;
}
}
printf("%d\n",count);
}
return 0;
}