码迷,mamicode.com
首页 > 编程语言 > 详细

O(nlogn)算法,最长上升子序列,,非动规

时间:2017-08-09 10:02:34      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:子序列   序列   while   规划   tac   一个   最快   return   include   

//最长上升子序列最快算法,非动态规划,运用了二分思想,还有栈的思想,
//用每一个数去和栈中的栈顶元素相比较,如果大于栈顶元素,则入栈,否则运用二分查找,寻找出第一个比这个数大的那个数替换
#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;
}

 

O(nlogn)算法,最长上升子序列,,非动规

标签:子序列   序列   while   规划   tac   一个   最快   return   include   

原文地址:http://www.cnblogs.com/oi-forever/p/7323246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!