Removed Interval

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1086    Accepted Submission(s): 392

Problem Description
Given a sequence of numbers A=a1,a2,…,aN, a subsequence b1,b2,…,bk of A is referred as increasing if b1<b2<…<bk. LY has just learned how to find the longest increasing subsequence (LIS).
Now that he has to select L consecutive numbers and remove them from A for some mysterious reasons. He can choose arbitrary starting position of the selected interval so that the length of the LIS of the remaining numbers is maximized. Can you help him with this problem?
 



Input
The first line of input contains a number T indicating the number of test cases (T≤100).
For each test case, the first line consists of two numbers N and L as described above (1≤N≤100000,0≤L≤N). The second line consists of N integers indicating the sequence. The absolute value of the numbers is no greater than 109.
The sum of N over all test cases will not exceed 500000.
 



Output
For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from 1. Y is the maximum length of LIS after removing the interval.
 



Sample Input
2
5 2
1 2 3 4 5
5 3
5 4 3 2 1
 



Sample Output
Case #1: 3
Case #2: 1
 



Source
 

题目大意:给你n个元素的序列。让你移除长度为L的一段区间,让剩下的元素所形成的LIS最大。

解题思路:枚举删除区间。同时更新删除区间后能形成的LIS最大值。并且每次更新删除区间的前面能形成的LIS。。。。。。。。。。。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+200;
const int INF = 0x3f3f3f3f;
int a[maxn],b[maxn],endminv[maxn];
int dp[maxn];
int BinSearch(int l,int r,int key,int typ){ //二分求解小于等于key的第一个位置
int md; //二分求解大于等于key的第一个位置
if(typ==0){
while(l<r){
md=(l+r)/2;
if(endminv[md]>key){
l=md+1;
}else if(endminv[md]<key){
r=md;
}else{
return md;
}
}
return l;
}else{
while(l<r){
md=(l+r)/2;
if(endminv[md]>key){
r=md;
}else if(endminv[md]<key){
l=md+1;
}else{
return md;
}
}
return l;
}
} int main(){
int T,n,L,cnt=0;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&L);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<=n;i++)
endminv[i]=-INF;
for(int i=n;i>=1;i--){ //逆序得到最长递减子序列,那么正序依然是递增的
int x=BinSearch(1,n,a[i],0);
endminv[x]=a[i];
dp[i]=x; //以a[i]结尾的最长递减子序列长度
}
memset(endminv,INF,sizeof(endminv));
int ans=0; int len=1;
for(int i=L+1;i<=n;i++){ //枚举要删除的区间的右端点
int x=BinSearch(1,n,a[i],1);
ans=max(ans,dp[i]+x-1);//删除区间后前后能拼接成的LIS长度
x=BinSearch(1,n,a[i-L],1); //枚举区间左边能形成的LIS
endminv[x]=a[i-L];
len=max(len,x+1); //对于区间左边正常求LIS
}
printf("Case #%d: %d\n",++cnt,max(ans,len-1));
}
return 0;
}

  

hdu 5489——Removed Interval——————【删除一段区间后的LIS】的更多相关文章

  1. 2015合肥网络赛 HDU 5489 Removed Interval LIS+线段树(树状数组)

    HDU 5489 Removed Interval 题意: 求序列中切掉连续的L长度后的最长上升序列 思路: 从前到后求一遍LIS,从后往前求一遍LDS,然后枚举切开的位置i,用线段树维护区间最大值, ...

  2. HDU 5489 Removed Interval (LIS,变形)

    题意: 给出一个n个元素的序列,要求从中删除任一段长度为L的连续子序列,问删除后的LIS是多少?(n<=10w, L<=n ,元素可能为负) 思路: 如果会O(nlogn)求普通LIS的算 ...

  3. HDU 5489 Removed Interval

    题意:求一段序列中删掉L个连续元素后的LIS. 解法:我的想法很复杂= =怎么说呢……首先用nlogn的方法求LIS得到的序列dp的第i项的意义为上升子序列所有长度为i的序列结尾元素的最小值,那么先倒 ...

  4. HDU 5489 Removed Interval (LIS变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5489 给你n个数,要删去其中连续的L个,问你删去之后的LIS最大是多少? 我们先预处理出以i下标为开头 ...

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

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...

  6. HDU 5489 Removed Interval DP 树状数组

    题意: 给一个长度为\(N\)的序列,要删除一段长为\(L\)的连续子序列,问所能得到的最长的\(LIS\)的长度. 分析: 设\(f(i)\)表示以\(a_i\)结尾的\(LIS\)的长度,设\(g ...

  7. HDU 5489 Removed Interval 2015 ACM/ICPC Asia Regional Hefei Online (LIS变形)

    定义f[i]表示以i为开头往后的最长上升子序列,d[i]表示以i为结尾的最长上升子序列. 先nlogn算出f[i], 从i-L开始枚举f[i],表示假设i在最终的LIS中,往[0,i-L)里找到满足a ...

  8. LIS(变形) HDOJ 5489 Removed Interval

    题目传送门 题意:求删掉连续L长度后的LIS 分析:记rdp[i]表示以a[i]为开始的LIS长度,用nlogn的办法,二分查找-a[i].dp[i]表示以a[i]为结尾并且删去[i-L-1, i-1 ...

  9. Hdu 5489 合肥网络赛 1009 Removed Interval

    跳跃式LIS(nlogn),在普通的转移基础上增加一种可以跨越一段距离的转移,用一颗新的树状数组维护,同时,我们还要维护跨越完一次后面的转移,所以我用了3颗树状数组.. 比赛的时候一句话位置写错了,然 ...

随机推荐

  1. Flask05 cookie、类视图、方法视图、自己的404页面

    1 什么是cookie 就是网站存放到你浏览器中的一部分固定内容:当你下次访问我这个网站的时候,你会把之前我存放到你浏览器中的数据带回来给我        你要先登录(用户名.密码) ->   ...

  2. 15、TSA数据上传(https://www.ncbi.nlm.nih.gov/genbank/tsaguide/#SP)

    https://www.ncbi.nlm.nih.gov/genbank/tsa/ https://www.ncbi.nlm.nih.gov/genbank/tsaguide              ...

  3. day1_2_3

    DD烧写命令(mfgtools-without-rootfs.tar.gz) ubuntu minicom svn 应用层进程阻塞调试 多机共享 securecrt的远程登录以及调试 tengxunt ...

  4. Python中使用json.loads解码字符串时出错:ValueError: Expecting property name: line 1 column 1 (char 1)

    解决办法,json数据只能用双引号,而不能用单引号

  5. Python版的数据库查询构造器、ORM及动态迁移数据表。

    Orator Orator提供一个简单和方便的数据库数据处理库. 它的灵感来源于PHP的Laravel框架,借助其思想实现了python版的查询构造器和ORM. 这是完整的文档:http://orat ...

  6. HDU 3915 Game (高斯消元)

    题意:有n堆石子,每个人只能从某一堆至少拿走一个,不能拿者败.问事先拿走某些堆的石子,使得先手必败. 析:将石子拆成二进制,未知数为1表示保留该堆石子,为0表示事先拿走该堆石子.最后求自由变元的数目, ...

  7. 【Qt官方例程学习笔记】Getting Started Programming with Qt Widgets

    创建一个QApplication对象,用于管理应用程序资源,它对于任何使用了Qt Widgets的程序都必要的.对于没有使用Qt Widgets 的GUI应用,可以使用QGuiApplication代 ...

  8. Web应用之ActionForm

    看链接: 主要就是讲了一下form表单的传递,bean的工作原理. http://blog.csdn.net/java_pengjin/article/details/5987330

  9. C# EventHandler委托事件小结--百度

    最近遇到一个委托的问题,+=这个符号 this.Activated += new EventHandler(Form1_Activated);//Form1_Activated为方法名12 这个语句拆 ...

  10. asp.net Page.Controls对象(找到所有服务器控件)

    前台 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="De ...