POJ - 2533 Longest Ordered Subsequence (longest monotonic sequence)

The meaning of problems:
the given length of the longest seek increased sequence length N
'' ideas: **

(. 1) \ (O (^ n-2) \) Dynamic Transfer:
set \ (dp [i] \) represented by \ (I \) up to the end of the monotone sequence, then when \ (arr [i]> when ARR [J] \) \ (DP [I] = max (DP [J] +. 1, DP [I]) (J \ in [. 1, I)) \)

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#define accept 0
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const int maxn = 1e3+7;
const int maxm = 1e6+7;
const int mod = 1e9+7;

int dp[maxn];
int arr[maxn];
int main(){
    int n;
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++){
            scanf("%d",&arr[i]);
            dp[i]  = 1;
        }
        
        int ans = -1;
        for(int i=1;i<=n;i++){
            for(int j=1;j<i;j++){
                if(arr[i]>arr[j]) dp[i] = max(dp[j]+1,dp[i]);
            }
            ans = max(ans,dp[i]);
        }
        printf("%d\n",ans);
    }
    return accept;
}

(2) \ (O (n-2 ^) \)
\ (D [I] of length = i + 1 \) incrementing a minimum value at the end of the subsequence (INF is not present)
beginning with INF array initialization dp value, considered from front to back and then the number of columns of elements, each for \ (a_j \) , if the \ (I \) in the first position or \ (a [j]> = a [i], \) such that \ ( a [j] = a [i ] \) and \ (BREAK \) , the last value of the first array subscript dp INF is the result of the
fact very similar monotonous stack, after the array sequentially shifted, if they dp is smaller than the previous one will be placed (to achieve a similar effect monotonic stack) dp is greater than the first value before, but still the longest length calculation.
Since the longest sequence to that from the perspective of the greedy to think if the sequence is long enough, we try to make the difference between each incremental minimum, so as to make it more values.

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#define accept 0
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const int maxn = 1e3+7;
const int maxm = 1e6+7;
const int mod = 1e9+7;

int n;
int arr[maxn];
int dp[maxn];
int main() {
    int n;
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++){
            scanf("%d",&arr[i]);
        }
        for(int i=1;i<=maxn;i++){
            dp[i] = inf;
        }
        dp[0] = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if(i == 1|| dp[j] >= arr[i])  {
                    dp[j] = arr[i]; break;
                }
            }
        }
        int ans = 0;
        while(dp[ans] != inf) ans++;
        printf("%d\n", ans-1);
    }
    return 0;
}

(3) \ (O (nlogn) \)
from the second method will be able to see it, look for the part, and then we do not have to traverse the comparison can be used to optimize the half

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#define accept 0
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
const int maxn = 1e3+7;
const int maxm = 1e6+7;
const int mod = 1e9+7;

int n;
int arr[maxn];
int dp[maxn];
int main() {
    int n;
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++){
            scanf("%d",&arr[i]);
            dp[i] = inf; 
        }
        dp[0] = 0;
        for(int i = 1; i <= n; i++) {
            *lower_bound(dp+1,dp+n+1,arr[i]) = arr[i];
        }
        int ans = lower_bound(dp+1,dp+1+n,inf) - dp;
        printf("%d\n", ans-1);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Tianwell/p/11415894.html