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. ios判断是否为iphone6或iphone6plus代码

    转自:http://blog.csdn.net/lvxiangan/article/details/45288505 #define IS_IPAD (UI_USER_INTERFACE_IDIOM( ...

  2. 排序----demo----

    排序1---冒泡法: 单向冒泡排序的基本原理就是:对于给定的n个数据,从第一个数据开始一次对相邻的两个数据进行比较,当前面的数据大于后面的数据时,交换位置,进行一轮比较和换位后,n个数据中最大的那个被 ...

  3. content-disposition attachment filename 在Firefox和IE中得到不同的结果

    在Firefox中需要把filename 用双引号包起来,才能得到想要的名字,不然如果含有空格,会丢掉空格后面的部分.而IE会把空格转为_,因此也需要HttpUtility.UrlPathEncode ...

  4. LAMP 1.5 测试PHP解析 问题解决

    安装完php从新加载了一个模块 php5_module 重新启动模块 /usr/local/apache2/bin/apachectl restart ifconfig 查看本机ip,在浏览器里面输入 ...

  5. vue.js2.0实战(1):搭建开发环境及构建项目

    Vue.js学习系列: vue.js2.0实战(1):搭建开发环境及构建项目 https://my.oschina.net/brillantzhao/blog/1541638 vue.js2.0实战( ...

  6. 8、泛型程序设计与c++标准模板库2、c++标准模板库中的容器

    顺序容器类以逻辑线性排列方式存储元素,在这些容器类型中的元素在逻辑上被认为是连续的存储空间中存储的.顺序容器可用于存储线性群体. 在关联容器类中,元素的存储和检索基于关键字和元素与其他元素之间的关系, ...

  7. Statement, PreparedStatement和CallableStatement的区别

    Statement用于执行不带参数的简单SQL语句,并返回它所生成的结果,每次执行SQL豫剧时,数据库都要编译该SQL语句. Satatement stmt = conn.getStatement() ...

  8. php获取request_uri

    urlParameters = http_build_query( filter_input_array( INPUT_GET, FILTER_SANITIZE_URL ) ); $_request_ ...

  9. NetCore 学习笔记(DI 实例生命周期)

    Transient: 每一次GetService都会创建一个新的实例 Scoped:    在同一个Scope内只初始化一个实例,同一个请求内只会被创建一次 Singleton :整个应用程序生命周期 ...

  10. html5表单及新增的改良元素

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...