最长上升子序列模版

#include<iostream>
#include<cstdio>
#define maxn 100005
#define INF 0x7fffffff
using namespace std;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int stack[maxn];
        int top=0,tempt;
        stack[top]=-1;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&tempt);
            if(tempt>stack[top])
            {
                stack[++top]=tempt;
            }
            else
            {
                int low=1,high=top,mid;
                while(low<=high)//用二分找比tempt大的第一个数
                {
                    mid=(low+high)/2;
                    if(stack[mid]<tempt)//这里得想清楚
                    {
                        low=mid+1;
                    }
                    else
                    high=mid-1;
                }
                stack[low]=tempt;
            }
        }
        printf("%d\n",top);
    }
	return 0;
}


   

你可能感兴趣的:(最长上升子序列模版)