AcWing mountaineering

It is organized by ACM's 2006 North a question.

Title means: Given attractions altitude, the team members do not visit the same height of attractions, beginning to climb, a climb down but it can no longer climb, find the maximum number of sights to see. Then we can get a conclusion: with a highest point of distinction is the biggest rise in front of the sequence, the latter is the biggest drop sequence. Then we complete the transfer of this question to LIS. We began to think about: a collection? Ending at the peak maximum length sequence (a 1-n; a n ~ 1). Attributes? Maximum. Division basis and calculated? In a [i] at the end of the sequence of a [1], a [2 ], a [i-1], a [i] was found a [j] <a [i [, then the state can be converted to the foregoing that the number of +1 state, can then be compared with the current. Of course, when a = 0 J, DP [I] =. 1 . Second, we must pay attention to reverse this time in reverse order to seek, again open a collection. The last peak point to enumerate.

Code

#include<bits/stdc++.h>
#define maxn 1010
using namespace std;
int dp1[maxn],dp2[maxn];
int n;
int a[maxn];
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=n;i++){
        dp1[i]=1;
        for(int j=1;j<i;j++){
            if(a[i]>a[j]){
                dp1[i]=max(dp1[i],dp1[j]+1);
            }
        }
    }
    for(int i=n;i>=1;i--){
        dp2[i]=1;
        for(int j=n;j>i;j--){
            if(a[i]>a[j]){
                dp2[i]=max(dp2[i],dp2[j]+1);
            }
        }
    }
    int res=0;
    for(int i=1;i<=n;i++){
        res=max(res,dp1[i]+dp2[i]-1);
    }
    cout<<n;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/china-mjr/p/11750025.html