首页 > 代码库 > 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)

【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)

题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=5489

题目大意:

  一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间),使得剩余的数最长上升子序列(LIS)最长。

题目思路:

  【二分】【最长上升子序列】

  首先,假设去掉[i,i+m-1]这L个数,剩余的LIS长度为max(i左端最后一个不大于a[i+m]的LIS长度+a[i+m]开始到最后的LIS长度)。

  所以,我们从n到1逆向先求最长下降子序列的长度f[i],就可以知道每个数开始到结束的LIS。

  接着,从头开始做LIS,同时二分在前i个数中找最后一位比a[i+m]小的最长的LIS长度,更新答案。

 

技术分享
  1 //  2 //by coolxxx  3 //#include<bits/stdc++.h>  4 #include<iostream>  5 #include<algorithm>  6 #include<string>  7 #include<iomanip>  8 #include<map>  9 #include<stack> 10 #include<queue> 11 #include<set> 12 #include<bitset> 13 #include<memory.h> 14 #include<time.h> 15 #include<stdio.h> 16 #include<stdlib.h> 17 #include<string.h> 18 //#include<stdbool.h> 19 #include<math.h> 20 #define min(a,b) ((a)<(b)?(a):(b)) 21 #define max(a,b) ((a)>(b)?(a):(b)) 22 #define abs(a) ((a)>0?(a):(-(a))) 23 #define lowbit(a) (a&(-a)) 24 #define sqr(a) ((a)*(a)) 25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) 26 #define mem(a,b) memset(a,b,sizeof(a)) 27 #define eps (1e-8) 28 #define J 10000 29 #define mod 1000000007 30 #define MAX 0x7f7f7f7f 31 #define PI 3.14159265358979323 32 #define N 100004 33 using namespace std; 34 typedef long long LL; 35 int cas,cass; 36 int n,m,lll,ans; 37 LL aans; 38 int a[N],f[N],q[N]; 39 void getf() 40 { 41     int i,l,r,mid; 42     lll=0; 43     for(i=n;i;i--) 44     { 45         l=0,r=lll; 46         while(l<r) 47         { 48             mid=(l+r+1)>>1; 49             if(q[mid]>a[i])l=mid; 50             else r=mid-1; 51         } 52         q[r+1]=a[i]; 53         f[i]=r+1; 54         lll=max(lll,r+1); 55     } 56 } 57 void work() 58 { 59     int i,l,r,mid; 60     lll=0; 61     for(i=1;i+m<=n;i++) 62     { 63         l=0,r=lll; 64         while(l<r) 65         { 66             mid=(l+r+1)>>1; 67             if(q[mid]<a[i+m])l=mid; 68             else r=mid-1; 69         } 70         ans=max(ans,r+f[i+m]); 71         l=0,r=lll; 72         while(l<r) 73         { 74             mid=(l+r+1)>>1; 75             if(q[mid]<a[i])l=mid; 76             else r=mid-1; 77         } 78         q[r+1]=a[i]; 79         lll=max(lll,r+1); 80     } 81     ans=max(ans,lll); 82 } 83 int main() 84 { 85     #ifndef ONLINE_JUDGE 86 //    freopen("1.txt","r",stdin); 87 //    freopen("2.txt","w",stdout); 88     #endif 89     int i,j,k; 90      91 //    for(scanf("%d",&cass);cass;cass--) 92     for(scanf("%d",&cas),cass=1;cass<=cas;cass++) 93 //    while(~scanf("%s",s+1)) 94 //    while(~scanf("%d",&n)) 95     { 96         ans=0; 97         printf("Case #%d: ",cass); 98         scanf("%d%d",&n,&m); 99         for(i=1;i<=n;i++)scanf("%d",a+i);100         getf();101         work();102         printf("%d\n",ans);103     }104     return 0;105 }106 /*107 //108 109 //110 */
View Code

 

【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)