Sample output 44597 Analysis: First sort by the keyword W in ascending order, then s [I] from 1 to I-1 (refers to the I element in the outer loop) Start traversing,
Locate W [I]> W [K], s [I] <s [K], set the maximum position of M [I], and record it. The dynamic planning state equation is: F [I] = max (F [k] + 1, F [I]), 1 <= k <I; Here we define a struct mice where Len represents the State length of the current element, used to find the longest, Index is used to remember the real subscript of the current element, and the subscript of the element can be found after sorting. The subscript of the previous element used by before to find the current element. # Include <iostream> # Include <stdlib. h> Using namespace STD ; Typedef struct { Int W , S ; Int Len , Index ; Int Before ; } Mice; Int CMP ( Const Void * A , Const Void * B ) { Mice C= *(Mice*)A; Mice d = *( Mice *) B ; If ( C . W = D. W ) Return D . S - C . S ; Else return C . W - D. W ; } Int Main () { Mice m [ 10001 ]; Int I = 1 , Flag , Max = 0 , F [ 1001 ]; While ( CIN > M[ I ]. W > M [ I ]. S ) { M[I].Index=I; M[I].Len=0; M[I].Before=0; I ++; } // Cout <I <Endl; Qsort ( M , I - 1 , Sizeof ( M [ 1 ]), CMP); For ( Int J = 1 ; J < I ; J ++) { For ( Int K = 1 ; K < J ; K ++) { If ( M[ J ]. W > M [ K ]. W && M [ J ]. S < M [ K ]. S ) { If ( M [ J ]. Len < M [ K ]. Len + 1 ) { M[J].Len=M[K].Len+1; M [ J ]. Before = K ; If ( M [ J ]. Len> = Max ) { Max=M[J].Len; Flag=J; } } } } } Cout<Max+1<Endl; F[1] =M[Flag].Index; I=2; While(M[Flag].Before! =0) { Flag=M[Flag].Before; F[I] =M[Flag].Index; I++; } For(IntJ=I-1;J> =1;J--) { cout F [ j ] Endl ;< BR >} // For (Int J = 1; j // cout return 0 ; } |