POJ 2533 求最長上升子序列長度 非DP 實現

#include<cstdio>
#include<algorithm>
#define maxn 1001
using namespace std;

int stack[maxn],N;

int main()
{
    scanf("%d",&N);

    int top=0;
    stack[0]=-1;
    for(int i=0;i<N;i++)
    {
        int temp;
        scanf("%d",&temp);
        if(stack[top]<temp)
        {
            stack[++top]=temp;
        }
        else
        {
            int low=1,high=top;
            int mid;
            while(low<=high)
            {
                mid=(low+high)/2;
                if(stack[mid] < temp)
                {
                       low=mid+1;
                }
                else
                {
                      high=mid-1;

                }
            }
            stack[low]=temp;


        }
    }
    printf("%d\n",top);
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章