poj1631

最长上升子序列,此题只能用O(nlogn)算法,具体思路在上一篇博客有详细介绍

#include <stdio.h>
int s[40005];
  int main()
  {
      int i,T,n,temp,top;
      scanf("%d",&T);
      while(T--)
      {
          scanf("%d",&n);
          top=0;
          s[top]=-1;
          for (i=1;i<=n;i++)
          {
              scanf("%d",&temp);
              if (temp>s[top])
                s[++top]=temp;
              else
              {
                  int low=1;
                  int high=top;
                  while(low<=high)
                  {
                      int middle=(low+high)/2;
                      if (s[middle]<temp)
                        low=middle+1;
                      else
                        high=middle-1;
                  }
                  s[low]=temp;
              }

          }
          printf("%d\n",top);
      }
      return 0;
  }


你可能感兴趣的:(poj1631)