1669: [usaco Oct] hungry cows hunger cows time limit: 5 sec memory limit: 64 MB
Submit: 665 solved: 419
[Submit] [Status] Descriptionfarmer John has n (1 <= n <= 5,000) cows, each of which has a positive integer no more than 32 bits. FJ hopes that the cows will be able to queue up in ascending order before eating, but the cows will never listen to him. In order for the cows to develop this habit, FJ picks out some of the cows in sequence each meal. The numbers of these cows must increase in the order of picking. Then FJ asked the cows picked out to eat-the other cows had to be hungry. Now you get the numbers of all the cows in the team before the meal. Please calculate how many cows can eat at most according to FJ's regulations? For example, 11 cows lined up in the following order (numbers represent the number of cows) 2 5 18 3 4 7 10 9 11 8 15 for this queue, A maximum of seven cows can have dinner. Their numbers are 2, 3, 4, 7, 10, 11, and 15. The queue 3rd, 15 is invalid because the number (3) of the first cows is smaller than the number (5) of the first one ). Input * row 1st: an integer, N * 2nd ..? Row: Except for the last row, each row contains exactly 20 integers separated by spaces, showing the number of the cows in the team from the front to the back. If n cannot be divisible by 20, the last line contains less than 20 output * 1st rows: the number of cows that can be picked out according to FJ rules sample input11
2 5 18 3 4 7 10 9 11 8 15
Sample output7hint
Question:
I can use it without O (nlogn), but I don't want to write it anymore. I just copied the code from the previous question.
Code:
1 var a,sta:array[0..5010] of longint; 2 i,top,n,j:longint; 3 function search(x:longint):longint; 4 var l,r,mid:longint; 5 begin 6 l:=1;r:=top; 7 while l<>r do 8 begin 9 mid:=(l+r) div 2;10 if sta[mid]>x then r:=mid else l:=mid+1;11 end;12 exit(l);13 end;14 function lis:longint;15 begin16 top:=1;sta[1]:=a[1];17 for i:=2 to n do18 begin19 if a[i]<sta[1] then j:=120 else if a[i]>=sta[top] then j:=top+121 else j:=search(a[i]);22 if j>top then begin inc(top);sta[top]:=a[i];end;23 if a[i]<sta[j] then sta[j]:=a[i];24 end;25 exit(top);26 end;27 begin28 assign(input,‘input.txt‘);assign(output,‘output.txt‘);29 reset(input);rewrite(output);30 readln(n);31 for i:=1 to n do read(a[i]);32 writeln(lis);33 close(input);close(output);34 end.
View code