码迷,mamicode.com
首页 > 其他好文 > 详细

LIS最长上升子序列 (nlogn) poj1631

时间:2015-08-13 21:57:56      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

问题描述:LIS(Longest Increasing Subsequence)最长上升(不下降)子序列,有两种算法复杂度为O(n*logn)和O(n^2)。在上述算法中,若使用朴素的顺序查找在D1..Dlen查找,由于共有O(n)个元素需要计算,每次计算时的复杂度是O(n),则整个算法的时间复杂度为O(n^2),与原来算法相比没有任何进步。但是由于D的特点(2),在D中查找时,可以使用二分查找高效地完成,则整个算法时间复杂度下降为O(nlogn),有了非常显著的提高。需要注意的是,D在算法结束后记录的并不是一个符合题意的最长上升子序列!算法还可以扩展到整个最长子序列系列问题。

有两种算法复杂度为O(nlogn)和O(n^2)

直接介绍O(nlogn)的算法:维护一个一维数组c,并且这个数组是动态扩展的,初始大小为1,c[i]表示最长上升子序列长度是i的所有子串中末尾最小的那个数,根据这个数字,我们可以比较知道,只要当前考察的这个数比c[i]大,那么当前这个数一定能通过c[i]构成一个长度为i+1的上升子序列。当然我们希望在C数组中找一个尽量靠后的数字,这样我们得到的上升子串的长度最长,查找的时候使用二分搜索,这样时间复杂度便下降了。

 

poj1631

题目大意:两岸的同等数目的信号灯一一对应,连线,从中找出最多的线出来,它们之间互不交叉。只要求输出最大的线条数。

 

早就会了   于是想想 感觉可以用set做,然而不知道为何TLE了。

技术分享
#include <iostream>
#include <cstring>
#include <cstdio>
#include <set>

using namespace std;

set<int> st;
set<int>::iterator it;

int main()
{
    int cas, n, t, ans;
    scanf("%d",&cas);
    while(cas--)
    {
        ans = 0;
        scanf("%d",&n);
        st.clear();
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&t);
            it = st.upper_bound(t);
            if(it != st.end()) st.erase(it);
                else ans++;
            st.insert(t);
        }
        printf("%d\n",ans);
    }
    return 0;
}
View Code

 

 

于是老实写了个二分,然后A了。

技术分享
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int MAXN = 40010;

int f[MAXN]; 

int main()
{
    int cas, n, t, ans;
    scanf("%d",&cas);
    while(cas--)
    {
        ans = 0;
        memset(f, 0, sizeof(f));
        scanf("%d",&n);
        int l, r, mid;
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&t);
            l = 0, r = ans-1;
            while(l <= r)
            {
                mid = (l + r)/2;
                if(f[mid] < t) l = mid + 1;
                else r = mid - 1; 
            }
            f[l] = t;
            if(l + 1 > ans) ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
View Code

 

 

还学了直接对数组用lower_bound();  orz

技术分享
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN = 40010;
int a[MAXN]; 

bool cmp(int x, int y) {
    return x<y;
}

int main()
{
    int cas, n, ans;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d",&n);
        ans = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
            int *p = lower_bound(a,a+ans,a[i],cmp);
            if(p == a+ans) ans++;
            *p = a[i];
        }
        printf("%d\n",ans);
    }
    return 0;
}
View Code

 

LIS最长上升子序列 (nlogn) poj1631

标签:

原文地址:http://www.cnblogs.com/Lucas666/p/4728496.html

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