BZOJ1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛

1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 665  Solved: 419
[Submit][Status]

Description

Farmer John养了N(1 <= N <= 5,000)头奶牛,每头牛都有一个不超过32位二进制数的正整数编号。FJ希望奶牛们在进食前,能按编号从小到大的顺序排好队,但奶牛们从不听他的话。为了让奶牛们养成这个习惯,每次开饭时,FJ从奶牛中顺序地挑出一些,这些奶牛的编号必须按挑出的顺序递增。然后FJ让被挑出的奶牛们吃饭——其他奶牛就只能饿肚子了。 现在,你得到了这一次开饭前队伍中从前到后所有奶牛的编号。奶牛们想请你计算一下,按照FJ的规定,最多有多少头奶牛能吃上饭? 比如说,有11头奶牛按以下顺序排好了队(数字代表奶牛的编号) 2 5 18 3 4 7 10 9 11 8 15 对于这个队列,最多可以让7头奶牛吃上饭,她们的编号分别为2,3,4,7,10,11,15。队列2,5,3,10,15是不合法的,因为第3头奶牛的编号(3)小于她前面一头奶牛的编号(5)。

Input

* 第1行: 一个整数,N * 第2..?行: 除了最后一行,每一行都包含恰好20个用空格隔开的整数,依次表 示队伍中从前到后的奶牛的编号。如果N不能整除20,那么最后一 行包含的数字不到20个

Output

* 第1行: 输出按照FJ的规定,最多可以挑出的奶牛的数目

Sample Input

11
2 5 18 3 4 7 10 9 11 8 15

Sample Output

7

HINT

题解:

不用O(nlogn)也能过吧,不过不想写了,直接照搬上一题的代码

代码:

 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   begin
16   top:=1;sta[1]:=a[1];
17   for i:=2 to n do
18    begin
19    if a[i]<sta[1] then j:=1
20    else if a[i]>=sta[top] then j:=top+1
21    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 begin
28   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

 

posted @ 2014-08-12 17:01  ZYF-ZYF  Views(215)  Comments(0Edit  收藏  举报