AcWing 187. missile defense system

// dp + + the DFS greedy
 // note of a global variable
#include<iostream>
the using  namespace STD;
 const  int N = 55 ;
 int n-;
 int ANS;
 int Q [N];
 int up [N], Down [N]; // represents a rise and fall of the sub-sequence end ending sequences 
void DFS ( int U , int SU, int SD) // U represents enumerate the first few figures, su represents the number of sub-sequences increase, sd denotes the number of sub-sequences fall 
{
     IF (SU + SD> = ANS) return ; // description has no longer small 
    IF (n-== U) // description has found a last 
    {
        years = su + nd;
        return ;
    }
    
    // current number into increased subsequence 
    int K = 0 ;
     the while (K <SU && up [K]> = Q [U]) K ++ ;
     int T = up [K]; // backup, for the following deep search site restoration back 
    up [K] = Q [U];
     IF (K <SU) DFS (U + . 1 , SU, SD);
     the else DFS (U + . 1 , SU + . 1 , SD);
    up [K] = T; // recovery site
     
    // current number dropped into the subsequence 
    K = 0 ;
     the while (K <&& Down SD [K] <= Q [U]) K ++ ;
    t=down[k];
    down[k]=q[u];
    if(k<sd)dfs(u+1,su,sd);
    else dfs(u+1,su,sd+1);
    down[k]=t; 
 } 
int main() {
    while(cin>>n,n) {
        for(int i=0; i<n; i++)
            cin>>q[i];
        ANS = n-; // assume that each number sequence is a 
        DFS ( 0 , 0 , 0 );
        cout<<ans<<endl;
    }
    return 0;
}

 

 

Guess you like

Origin www.cnblogs.com/QingyuYYYYY/p/11961179.html