Longest rising substring

Source: Internet
Author: User

N ^ 2Solution

For a [I], J in 0 .. i-1, if a [I]> A [J], DP [I] = DP [J] + 1; otherwise DP [I] is at least 1

For i in 0..n  For j in 0..i-1    If(a[i] > a[j]) dp[i] = max(dp[i],dp[j]+1);  dp[i] = max(1,dp[i]);

 

 

NlognSolution

Http://www.ahathinking.com/archives/117.html

Purpose: we expect to find such a sequence in all the incremental sub-sequences with the length of Len In the first I element. Its maximum element is smaller than that of ARR [I + 1, and the length should be as long as possible. In this way, we only need to record the minimum value of the maximum element in the increasing sub-sequence of Len length to make the future increasing sub-sequence as long as possible.

Method: maintain an array of maxv [I], record the minimum value of the maximum element in the ascending subsequence with the length of I, and check for each element in the array which is the largest element of the subsequence, in the binary update maxv array, the final I value is the length of the longest incrementing sub-sequence. This method is really clever.

Maxv [] must be an incremental array. The maximum number (that is, the last number) of records in the I sequence is

For example, a [] = {1,-1, 2,-3, 4,-5, 6, 7,

When I is 1, maxv [0] = 1 at the beginning;

When I is 2, then maxv [0] =-1, because-1 <1, replace 1 with-1 to get better results

When I is 3, maxv [1] = 2. Because 2>-1, 2 can be connected to the end of-1 to form a longer array.

When I is 4, maxv [0] =-3, because-3 <-1, replacing-1 with-3 can achieve better results.

........

Therefore, if we can quickly replace a [I] with K in maxv [] and then use a [I] to replace K, we can achieve better results, if the number in maxv [] is smaller than a [I], it means that a longer sequence can be obtained, and maxv [++ Len] = A [I]

for i in 0..n do
{
if(a[i] > MaxV[len-1]) /* 大于最大的自然无需查找,否则二分查其位置 */ MaxV[len++] = a[i]; else { int pos = BinSearch(MaxV,len,a[i]);//相当于lower_bound, 返回 MaxV[i]中刚刚大于x的那个元素的下标 MaxV[pos] = a[i]; }
}

 

 

The following are several detailed codes:

#include <iostream>using namespace std;/* 最长递增子序列 LIS * 设数组长度不超过 30 * DP + BinarySearch*/int MaxV[30]; /* 存储长度i+1(len)的子序列最大元素的最小值 */int len;      /* 存储子序列的最大长度 即MaxV当前的下标*//* 返回MaxV[i]中刚刚大于x的那个元素的下标 */int BinSearch(int * MaxV, int size, int x){    int left = 0, right = size-1;    while(left <= right)    {        int mid = (left + right) / 2;        if(MaxV[mid] <= x)        {            left = mid + 1;        }
else { right = mid - 1; } } return left;}int LIS(int * arr, int size){ MaxV[0] = arr[0]; /* 初始化 */ len = 1; for(int i = 1; i < size; ++i) /* 寻找arr[i]属于哪个长度LIS的最大元素 */ { if(arr[i] > MaxV[len-1]) /* 大于最大的自然无需查找,否则二分查其位置 */ { MaxV[len++] = arr[i]; }
else { int pos = BinSearch(MaxV,len,arr[i]);//相当于lower_bound, 返回MaxV[i]中刚刚大于x的那个元素的下标 MaxV[pos] = arr[i]; } } return len;} void main(){ int arr[] = {1,-1,2,-3,4,-5,6,-7}; /* 计算LIS长度 */ printf("%d\n",LIS(arr,sizeof(arr)/sizeof(int)));}

 

Use the lower_bound Function

Foj   1438实在是太恶心了。。。。 #include<stdio.h>#include<stdlib.h>#include<iostream>#include<string.h>#include<queue>#include<algorithm>using namespace std;#define LL __int64#define M 220100const int INF = 2139062143;const int mod = 1000000007;int __ = 0;#define BIAOJI printf("yes%d\n",++__); int f[M] = {0};int a[M] = {0},n,nb = 0;int b[M] = {0};  int main(){     freopen("in.txt","r",stdin);    while(cin>>n)    {     memset(f,0,sizeof(f));    memset(a,0,sizeof(a));    memset(b,0,sizeof(b));    for(int i = 1;i<=n;++i)scanf("%d",&a[i]);    f[1] = 1;    b[1] = a[1];    nb = 1;     for(int i = 2;i<=n;++i)    {        int kk = lower_bound(b+1,b+nb+1,a[i]) - b;        if(kk == nb+1) ++nb;        b[kk] = a[i];        f[i] = kk;         //PP();    }    int ret = 0;    for(int i = 1;i<=n;++i) ret = max(ret,f[i]);    cout<<ret<<endl;     }     return 0; } /*int main(){     freopen("in.txt","r",stdin);    while(cin>>n)    {        int ans = 0;    memset(a,0,sizeof(a));     for(int i = 0;i<n;++i)scanf("%d",&a[i]);     int *b=new int [n+1];         b[ans=0]=-0x7fffffff;         for(int i=0;i<n;i++){                   int k=lower_bound(b,b+ans+1,a[i])-b;//upper_bound for Longest Non Descending Sub Sequence;                   if (k>ans) b[++ans]=a[i];        else if (b[k]>a[i]) b[k]=a[i];         }    cout<<ans<<endl;     }     return 0; }*/

 

Longest rising substring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.