The Pirate Kidd is a legendary thief with a special focus on jewelry as a super thief. His most prominent place was that he was able to escape the heavy containment of the village police, which was largely thanks to the easy-to-operate glider he was carrying.
One day, the strange thief Kidd as usual stole a precious diamond, but was Conan children saw through the camouflage, and his glider wing of the power plant was Conan kicked out of the football destroyed. The last resort was to escape from the damage of the gliding wing only.
Suppose a total of n buildings in a city are lined up in a single line, each building has a different height. At the beginning, The strange thief Kidd can be at the top of any building. He can escape in one direction, but cannot change direction halfway (because the senjing will chase behind). As the glider was damaged, he could only glide down (ie: only from the higher building to the lower building). He wants to go through the top of different buildings as much as possible, which can slow down the impact and reduce the likelihood of injury. May I ask how many different buildings he can pass at the top (including the original building)?
669
When we see the topic, the first reaction is the longest descent subsequence, in fact, this is really the longest descent subsequence, but we have to note that at the beginning, The strange thief Kidd can be at the top of any building. He can escape in one direction, but cannot change direction halfway (because the senjing will chase behind). Hey? What does this mean, not necessarily the two-way Ah! Well, think of here, you have the approximate algorithm to design out, yes, the two-way longest descending sub-sequence ... (The above name, purely self-created, if there is a similar, purely coincidental). So the next step is the code implementation problem. The first is the two-way storage problem, we can open a a[2][101] to record the height, wherein, a[1][i] represents the height from left to right to the first building, A[2][i] represents the height from right to left the first building. Similarly, we set f[2][101] to record the number of buildings that pass from left to right (from right to left) to the first building, and then sweep the F array once to find the maximum value. The next step is the code.
1 ProgramNOI4977;2 var3F:Array[1..2,0..101] ofLongint;4A:Array[1..2,0..101] ofLongint;5 I,j,k,n,m,max:longint;6 begin7 Readln (m);8 fork:=1 toM Do9 beginTenFillchar (F,sizeof (f),0); One READLN (n); A fori:=1 toN Do - begin -Read (a[1, I]); thea[2, n-i+1]:=a[1, I]; - End; -f[1, n]:=1; -f[2, n]:=1; + fori:=n-1 Downto 1 Do - begin +max:=0; A forj:=i+1 toN Do at if(a[1, i]>a[1, j]) and(f[1, J]>max) Then -max:=f[1, j]; -f[1, i]:=max+1; - End; - fori:=n-1 Downto 1 Do - begin inmax:=0; - forj:=i+1 toN Do to if(a[2, i]>a[2, j]) and(f[2, J]>max) Then +max:=f[2, j]; -f[2, i]:=max+1; the End; *max:=0; $ fori:=1 toN DoPanax Notoginseng if(max<f[1, I]) Then -max:=f[1, I]; the fori:=1 toN Do + if(max<f[2, I]) Then Amax:=f[2, I]; the Writeln (max); + End; - End.