【动态规划】【二分】【最长不下降子序列】洛谷 P1020 导弹拦截

最长不下降子序列的nlogn算法 见 http://www.cnblogs.com/mengxm-lincf/archive/2011/07/12/2104745.html

这题是最长不上升子序列,倒过来当最长不下降子序列搞就行。

若是最长上升子序列,将upper_bound改成lower_bound即可。

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 int n,en=1,d[100001],b[100001],k=1,t[100001],l[100001];
 5 int* p;
 6 int main()
 7 {
 8     while(scanf("%d",&t[n+1])!=EOF) n++;
 9     for(int i=1,j=n;i<=n;i++,j--) d[i]=t[j];
10     l[1]=t[1];
11     for(int i=2;i<=n;i++)
12       {
13           int x=0;
14           for(int j=1;j<=k;j++)
15             if(l[j]>=t[i])
16               {
17                 if(!x) x=j;
18                 else if(l[j]<l[x]) x=j;
19               }
20           if(!x) l[++k]=t[i];
21           else l[x]=t[i];
22       }
23     b[1]=d[1];
24     for(int i=2;i<=n;i++)
25       {
26           p=upper_bound(b+1,b+en+1,d[i]);
27           if(!(*p)) en++;
28           (*p)=d[i];
29       }
30     printf("%d\n%d\n",en,k);
31     return 0;
32 }
posted @ 2014-10-28 08:42  AutSky_JadeK  阅读(289)  评论(0编辑  收藏  举报
TVアニメ「Charlotte(シャーロット)」公式サイト TVアニメ「Charlotte(シャーロット)」公式サイト