1 // 2 // Created by snnnow on 2020/4/13. 3 // 4 // This is the basic problem of dp problem 5 // 6 // The longest does not drop 7 // (Missile interception is its example) 8 // So what is this article about, 9 // Mainly, this question uses a two-dimensional array, 10 // And the missile used three one-dimensional arrays at the time 11 // In fact, it is essentially the same 12 // (There is a structure to do it! QAQ [Manual dog head]) 13 14 // Not much to say, ans [i] [1] is the original number 15 // ans [i] [2] is the item The longest 16 // ans [i] [3] points to the next (the position of the next value) 17 //Start! 18 #include <iostream> 19 using namespace std; 20 int main () { 21 int ans [ 10010 ] [ 10 ]; // Although we only need three numbers in the second dimension, it is better to open a larger 22 int n; 23 cin >> n; 24 for ( int i = 1 ; i <= n; ++ i) { 25 cin >> ans [i] [ 1 ]; 26 ans [i] [ 2 ] = 1 ; 27 ans [ i] [ 3 ] =0 ; 28 29 } 30 for ( int j = n- 1 ; j> = 1 ; -j) { 31 int k = 0 ; // Note that k and p are updated every time i, so must Put in this loop here 32 int p = 0 ; 33 for ( int i = j + 1 ; i <= n; ++ i) { 34 35 if (ans [j] [ 1 ]> = ans [i] [ 1 ] && ans [j] [ 2 ]> k) { // k records the largest 36 k of all ans [j] [2] = ans [j] [2 ]; 37 38 p = j; 39 } 40 if (k> 0 ) { 41 ans [i] [ 3 ] = p; // p is a "pointer", ans [j] [3] stores a Position 42 ans [i] [ 2 ] = k + 1 ; 43 } 44 45 } 46 47 } 48 // Compare ans [i] [2] one by one to find the largest one, pay attention to find a variable and mark it 49 int mark = 1 ; 50 for ( int i =1 ; i <= n; i ++ ) { 51 if (ans [i] [ 2 ]> = ans [mark] [ 2 ]) { 52 mark = i; // This is not a sort, no double loop is needed, just find a comparison with what has been on the line 53 is } 54 is } 55 COUT << ANS [Mark] [ 2 ] << endl; 56 is the while (! Mark = 0 ) { 57 is COUT << " " << ANS [Mark] [ . 1 ]; 58 mark = ans [mark] [ 3 ]; 59 } 60 return 0 ; 61 }
This time, the first reverse order loop -j is written as ++ j. . Directly fried
Then write the definition of k in the wrong place (k is updated with each ans [i])
Too silly, Xiaobai will continue to cheer!