首页 > 代码库 > NOIP 考前DP 复习

NOIP 考前DP 复习

POJ 2533 最长不降子序列

技术分享
 1 #include <cstdio> 2 const int Maxn=1010; 3 int a[Maxn],Pos[Maxn],F[Maxn],n,Ans; 4 inline int Max(int x,int y) {return x>y?x:y;} 5 inline int Find(int x) 6 { 7     int l=1,r=Ans,Res; 8     while (l<=r) 9     {10         int mid=(l+r)>>1;11         if (a[Pos[mid]]<x) Res=mid,l=mid+1;  else r=mid-1;12     }13     return Res;14 }15 int main()16 {17     scanf("%d",&n);18     for (int i=1;i<=n;i++) scanf("%d",&a[i]);19     F[1]=1; Pos[1]=1; Ans=1;20     for (int i=2;i<=n;i++)21     {22         int t=Find(a[i]);23         Pos[t+1]=i;24         F[i]=t+1;25         Ans=Max(Ans,F[i]);26     }27     printf("%d\n",Ans);28     return 0;29 }
POJ 2533

 

NOIP 考前DP 复习