LGp1020- missile interceptors

Topic Link

Title required output up to the number of interceptor missiles and missile interceptor entire system with a minimum number;

Apparent up to the number of missile intercept is the longest non-rising sequence; Analysis of all available interceptor missile system with a minimum increase in number of the number of the longest sequence;

The system is provided with a number of a minimum, there is a non-rising sequence, wherein the two sequences either take c, d, C must be present in a number in a number larger than d, or c, d should be classified as a sequence, then we can from these sequences in a number to give a longest depicting a rising sequence, if this sequence is smaller than the number of sub-rise means that the subject has the same number of two to increase the maximum number of sub-sequence of m original sequence a non-rising sequence, not meet the definition of a non-rising sequence, so a = m;

Solution 1: dynamic programming (time complexity of O (n- 2 ))

#include<iostream>
using namespace std;
int h[100005],d[100005],a[100005];
int main(){
    int n=0,ans1=0,ans2=0;
    while(cin>>a[n++]);n--;
    for(int i=0;i<n;i++) {h[i]=1;d[i]=1;}
    for(int i=1;i<n;i++)
        for(int k=0;k<i;k++){
            if(a[i]>a[k]&&h[i]<h[k]+1)
            h[i]=h[k]+1;
            if(a[i]<=a[k]&&d[i]<d[k]+1)
            d[i]=d[k]+1;
        }
        for(int i=0;i<n;i++){
            ans1=max(ans1,d[i]);
            ans2=max(ans2,h[i]);
        }
        cout<<ans1<<endl<<ans2<<endl;
return 0;
}

Solution 2: lower_bound (), upper_bound ( ) ( time complexity O (nlog 2 n-))

lower_bound (), upper_bound () is a binary search, due to increased non-required sequence, so upper_bound, () added great <int> i.e. to find the array corresponding to the first address number less than

#include<iostream>
#include<algorithm>
using namespace std;
int h[100005],d[100005],a[100005];
int main(){
    int n=0,ans1=0,ans2=0;
    while(cin>>a[n++]);n--;
    h[0]=a[0];d[0]=a[0];
    for(int i=1;i<n;i++){
        if(a[i]<=d[ans1])
            d[++ans1]=a[i];
        else
            *upper_bound(d,d+ans1+1,a[i],greater<int>())=a[i];
        if(a[i]>h[ans2])
            h[++ans2]=a[i];
        else
            *lower_bound(h,h+ans2+1,a[i])=a[i];

    }
        cout<<ans1+1<<endl<<ans2+1<<endl;
return 0;
}

  

 

Guess you like

Origin www.cnblogs.com/WELOTX/p/11297620.html