复制代码

LuoGuP1020 导弹拦截

传送门

lcs问题  O(n^2) 暴力搜索 就TLE 了

所以直接 二分就好了维护第i 小 的数字 是多少

 

#include<bits/stdc++.h>
using namespace std;
#define LOACL  freopen("in","r",stdin);\
            freopen("out","w",stdout); 

const int  inf = 987654321;
const int sz = 1e6 + 5;
const int mod = 1e9 + 7;
const int sqrtn = 300; 

#define add(u,v,w) (e[++tot]=(edge){v,head[u],1},head[u]=tot;) 
#define f(i,l,r) for(int i=(int)l;i<=(int)r;++i)
#define g(i,l,r) for(int i=(int)l;i>=(int)r;--i)
#define CLR(arr,val) memset(arr,val,sizeof(arr)) 
typedef long long ll; 

 
 int a[sz], b[sz];
 int n,l,r,m;
 int main()
 {
     LOACL
     while(cin>>a[++n]);
     n--;
     //1 ______ n
     int ans = 0;
     b[0]=inf;
     f(i,1,n)
     {
         if(b[ans]>=a[i])
         {
             b[ans+1]=a[i];
             ans++;
         }
         else 
         {
             l=0;r=ans;
             while(l<r)
             {
                 m=(l+r)>>1;
                 if(b[m]>=a[i]) l=m+1;
                 else r=m;
             }
             if(l!=0)b[l]=a[i];
         } 
     }
     cout<<ans<<endl;
     CLR(b,-1);
     ans=0;
     f(i,1,n)
     {
         if(b[ans]<a[i])
         {
             b[ans+1]=a[i];
             ans++;
         }
         else 
         {
             l=0,r=ans;
             while(l<r)
             {
                 m=(l+r)>>1;
                 if(b[m]>=a[i])r=m;
                 else l= m+1;
             }
             if(l!=0)
             b[l]=a[i];
         }
     }
     cout<<ans<<endl;
     return 0;
 }
View Code

 

posted @ 2018-03-16 11:57  pg633  阅读(120)  评论(0编辑  收藏  举报