Luo Gu [P1233] stick processing solution to a problem

Algorithm: Sort, DP (rising longest sequence)

Preface:

This data is very water problem, given a set of data hack here:

21
96 25 1 9 39 19 87 51 7 61 11 1 46 74 51 1 1 61 51 84 51 76 49 33 13 57 73 86 41 99 9 81 41 51 13 61 17 33 81 62 47 41 

Strengthen data request!

------------

text

I see the solution to a problem in a lot of people are greedy write only praise a relatively large number of DP solution was also my data hack, so he wrote this solution to a problem (in fact the first time I submitted the code will be hack).

This question is the first to use ordered structure to stick the length of a first key (descending), a width of a second sort key (the same is descending).

Note that, if in the case of two sticks of equal length, the width must Yaoan sorting, or be hack.

After sorting, we can directly throw this regardless of the length of, directly over the width of the longest run sequence does not rise, the number of stars is not raised up sequence. But we know, do not rise up to the number of the sub-sequence is equal to the length of the longest sub-sequence of rising, so we can only claim the latter.

$ \rm code $

The include # <. Bits / STDC H ++>
 the using  namespace STD; 
# DEFINE maxN 50005 
struct Stick {
     int L, W; 
} Stick [maxN]; 
// first define a name for the structure Stick, storage stick length and width . 
int n, dp [maxN];
 // then define the number n of the stick and dp array. 
BOOL CMP (Stick, Stick);
 // this is the sort function. 
int main () {
 //     the freopen ( "1.In "," R & lt ", stdin); 
    CIN >> n-;
     for ( int I = . 1 ; I <= n-; I ++) >> CIN Stick [I] .L >> Stick [I] .W; 
    Sort ( Stick + 1, Stick + . 1 + n-, CMP);
     // input data sorting. 
    Dp [ . 1 ] = Stick [ . 1 ] .W;
     // . Initialization dp array 
    int K = . 1 ; // answer is also minimized 1. 
    for ( int I = 2 , J; I <= n-; I ++) J = lower_bound (DP + . 1 , K + DP + . 1 , Stick [I] .W) - DP, J <DP = K [J]? Stick = [I] .W: DP [K ++] = Stick [I] .W;
     // run again rising up sequence. 
    COUT << << K ' \ n- ' , Exit ( 0 );
     // output answer. 
}
BOOL CMP (A Stick, Stick B) {
     IF (! Al = BL) return Al> BL; // if two sticks of unequal length, then the length of the stick according to the descending order. 
    return AW> BW; // otherwise, according to the width of a stick of descending order. 
}

 

Guess you like

Origin www.cnblogs.com/Xray-luogu/p/11006416.html