A missile interception system has been developed by a country to defend against enemy missile attacks. But the missile interception system has a flaw: although its first artillery shells can reach any height, each projectile cannot be higher than the previous one. One day, the radar caught the enemy's missiles to attack. Since the system is still in the trial phase, there is only one set of systems that may not intercept all missiles.
The height of the input missiles in turn (the radar gives a positive integer of not more than 30000), calculates how many missiles the system can intercept, and outputs the minimum number of such missile interceptors to be equipped to intercept all missiles.
First ask, very simple, longest not ascending subsequence + priority queue, O (NLOGN)
The second question, can be converted into the longest ascending subsequence (oneself want), time complexity ibid.
1 var2 s,s2:string;3 I,j,t,n,tail:longint;4A,queue:Array[0..10000] ofLongint;5 functionPosdown (value:longint): Longint;6 var7 Middle,left,right:longint;8 begin9left:=1;Tenright:=tail+1; One whileLeft<right Do A begin -middle:= (Left+right)Div 2; - ifQueue[middle]>=value ThenLeft:=middle+1 the Elseright:=Middle; - End; -posdown:=Left ; - End; + functionPosup (value:longint): Longint; - var + Middle,left,right:longint; A begin atleft:=1; -right:=tail+1; -queue[right]:=Maxlongint; - whileLeft<right Do - begin -middle:= (Left+right)Div 2; in ifQueue[middle]<value ThenLeft:=middle+1 - Elseright:=Middle; to End; +posup:=Left ; - End; the begin * Readln (s); $s:=s+' ';Panax Notoginsengj:=1; - fori:=1 toLength (s) Do the begin + ifs[i]=' ' Then A begin the Inc (n); +S2:=copy (s,j,i-j); - Val (s2,a[n],t); $j:=i+1; $ End; - End; -tail:=0; the fori:=1 toN Do - beginWuyir:{Posdown (A[i]); thequeue[t]:=A[i]; - ifT>tail ThenInc (tail); Wu End; - writeln (tail); Abouttail:=0; $Fillchar (queue,sizeof (t),0); - fori:=1 toN Do - begin -t:=Posup (A[i]); Aqueue[t]:=A[i]; + ifT>tail ThenInc (tail); the End; - writeln (tail); $ End.
Missile interception--optimization algorithm