Chorus formation algorithmic problems records (Gangster code C ++, but mainly to see the problem-solving ideas)

A cow off the machine Huawei online questions, not thinking after reading, then read a person's explanation, I feel very good idea, here the record about the topic as follows:

How many students calculate the least out of the line so that the rest of the students lined up choral formation

Description:

N bits students stand in a row, wherein the music teacher to ask (NK) students dequeue bits, K bits such that the remaining students chorus aligned formation. 
Chorus formation refers to a formation: students set K-bit from left to right are numbered 1,2 ..., K, respectively, their height T1, T2, ..., TK, the presence of their height satisfies i (1 <= i <= K ) such that T1 <T2 <...... <Ti-  1 <Ti> Ti + 1> ......> TK.
Your task is known to all students of the N-bit height to calculate a minimum of several students out of the line, you can make the rest of the students lined up chorus formation. 

My Additional note: the rest of the people that you are not allowed to adjust their positions. This is also the theme of this most difficulty, if allowed to adjust, it is so easy a (maximum of only one, at most two other values, it is a simple question of removing too much of the data ...)

eg:

Input:

8
186 186 150 200 160 130 197 200

Output:

4

 

Ideas are as follows:

First, calculate the position of each number in the maximum incremental substring

186  186  150  200  160  130  197  200   quene

11122134 counts

 

Is then calculated for each position of the maximum number of reverse sub-string down ---> maximum number of incremental position of each substring of the reverse calculation

200,197,130,160,200 150 186 186 Reverse quene

11123233 count down

 

Each number is then incremented down count adding

186  186  150  200  160  130  197  200   quene

11122134 counts

33232111 counting down

44354245 where each number in the queue number +1 (increment themselves down and be counted in)

 

As this number 160

Number 2 person in the queue is incremented

150  160

2 person in the reduced number queue

160  130

So there are three people in the queue 160 is located

150  160  130

 

The number of queue where each number is the meaning of the expression

 

The total number - the number of people queue = number where a number of teams needed

#include <the iostream> 
#include <Vector> 
#include <algorithm>
 the using  namespace STD; 
 
void calIncSub (Vector < int > Quene, Vector < int > & the Num) {
     for ( int I = . 1 ; I <quene.size () ; I ++ )
         for ( int J = I- . 1 ; J> = 0 ; J, )
             IF (Quene [J] <Quene [I] && the Num [I] <the Num [J] + . 1 )   // find the ratio of the front small current, and the maximum substring [count] can be obtained by 
                the Num [I] = the Num [J] + . 1 ; 
}
 
int main(){
    int n;
    int h;
     
    while(cin>>n){
        vector<int> quene;
        vector<int> incNum(n,1);  //初始化为n个1
        vector<int> decNum(n,1); 
        vector<int> totalNum;
        for(int i=0;i<n;i++){
            cin >> h;
            quene.push_back(h);   
        }
        calIncSub (Quene, incNum);     // find the sub-burst count is incremented 
        Reverse (quene.begin (), quene.end ()); // inverted, i.e., to find the reverse substring count 
        calIncSub (Quene, decNum); 
        Reverse ( decNum.begin (), decNum.end ());    // reverse forward i.e. increment decrement 
        int max = 0 ;
         for ( int I = 0 ; I <n-; I ++ ) { 
            totalNum.push_back (incNum [I] + decNum [I]);
             IF (totalnum [I]> max) 
                max = totalnum [I]; 
        } 
        COUT <<-n-max + . 1 <<endl;
    }  
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/gsp1004/p/11117822.html