Specific Regulation movable - decrease maximum length sequence (LIS)

LIS bare title: https://vjudge.net/problem/HDU-1257

The first time I do not think this problem and move with the regulations, the stack can be directly used for maintenance monotonous
searching the first of which is less than its elements can be used upper_bound () binary search O ( l O g n ) O (logn)
Complexity O ( n l O g n ) O (nlogn)

0ms

/**
 * HDU 1257
 * LIS (single stack)
 * @author Hongchuan CAO
 */

#include<iostream>
#include<algorithm>
using namespace std;

int n;
int arr[10000];
int single[10000];


void get_data(){
    for(int i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }

}

void solve(){
    int maxx = -1; 
 
    int last = 0;
    for(int j=0;j<n;j++){
        int index = last;
        while(index>0&&arr[j]<single[index]){
            index--;
        }
        single[++index] = arr[j];
        last = max(index,last);
        maxx = max(maxx,last);
    }
    printf("%d\n",maxx);
}

int main(){
    while(scanf("%d",&n)!=EOF){
        get_data();
        solve();
    }

    return 0;
}

Upper_bound optimization with this:

/**
 * HDU 1257
 * LIS (single stack)
 * @author Hongchuan CAO
 */
#include<iostream>
#include<algorithm>
using namespace std;

int n;
int arr[10000];
int single[10000];

void get_data(){
    for(int i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }

}

void solve(){
    int maxx = -1; 
    int index = 0;
    for(int j=0;j<n;j++){
        if(arr[j]>single[index]){
            single[++index] = arr[j];
        }else{
            int change = upper_bound(single,single+index+1,arr[j])-single;
            single[change] = arr[j]; 
        }
        maxx = max(maxx,index);
    }
    printf("%d\n",maxx);
}

int main(){
    while(scanf("%d",&n)!=EOF){
        get_data();
        solve();
    }
    return 0;
}

Use dynamic rules to resolve:
direct violence search
31ms

/**
 * HDU 1257
 * 
 * @author Hongchuan CAO
 */

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;

int n;
int arr[10000];
int dp[10000];


void get_data(){
    for(int i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
}

void clear(){
    for(int i=0;i<10000;i++) dp[i] = 1;
}

void solve(){
    int maxx = -1;
 
    for(int i=1;i<n;i++){
        for(int j=i-1;j>=0;j--){
            if(arr[i]>arr[j]){
                dp[i] = max(dp[i],dp[j]+1);
                maxx = max(maxx,dp[i]);
            }
        }
    }
    printf("%d\n",maxx);
}

int main(){
    while(scanf("%d",&n)!=EOF){
        clear();
        get_data();
        solve();
    }

    return 0;
}

Guess you like

Origin blog.csdn.net/baidu_41560343/article/details/92433725