题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5489

给你n个数,要删去其中连续的L个,问你删去之后的LIS最大是多少?

我们先预处理出以i下标为开头的LIS,存到数组中。

然后可以枚举长为L的区间,每次移动,左边增加一个,右边删除一个。

最长上升子序列长度 = 窗口右边以右边第一个元素开头的最长上升子序列 + 窗口左边最大元素小于窗口右边第一个元素的最长上升子序列。

比如 1 2 [4 3] 2 3 5  ,  LIS = 3 + 1 = 4

求以i开头的LIS 只要倒着求最长下降子序列 or 倒着a[i]变负求最长上升子序列即可。

代码写的有点乱。

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e5 + ;
int dp1[N], dp2[N], a[N], inf = 1e9 + ;
int y[N], x[N]; // y[i]表示以i下标为开头的LIS,x[i]表示以i为结尾的LIS int main()
{
int t, n, m;
scanf("%d", &t);
for(int ca = ; ca <= t; ++ca) {
scanf("%d %d", &n, &m);
for(int i = ; i <= n; ++i) {
scanf("%d", a + i);
dp1[i] = dp2[i] = inf;
}
dp2[] = dp1[] = inf;
int ans = ;
for(int i = n; i >= ; --i) { //倒着求变负,求以i开头的LIS
int pos = lower_bound(dp2, dp2 + n, -a[i]) - dp2;
y[i] = pos + ;
//cout << y[i] << endl;
dp2[pos] = -a[i];
if(i == m + ) {
ans = lower_bound(dp2, dp2 + n, inf) - dp2;
}
}
//cout << ans << endl;
for(int i = ; i <= n; ++i) {
int pos = lower_bound(dp1, dp1 + n, a[i]) - dp1;
x[i] = pos + ;
dp1[pos] = a[i];
if(i == n - m) {
ans = max(int(lower_bound(dp1, dp1 + n, inf) - dp1), ans);
break;
}
}
//cout << ans << endl;
printf("Case #%d: ", ca);
dp1[] = inf;
for(int i = ; i <= n; ++i) {
dp1[i] = inf;
}
for(int i = m + ; i <= n; ++i) {
int pos = lower_bound(dp1, dp1 + n, a[i] - ) - dp1; //求滑窗左边刚好小于滑窗右边第一个数的LIS
ans = max(ans, y[i] + (a[i] - == a[pos] ? pos + : pos));
*lower_bound(dp1, dp1 + n, a[i - m]) = a[i - m];
}
printf("%d\n", ans);
}
return ;
}

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】

    Removed Interval Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

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

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

  4. 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 ...

  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

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

  7. HDU 5489 Removed Interval DP 树状数组

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

  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 5256 序列变换 (LIS变形)

    序列变换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

随机推荐

  1. 51nod1711 平均数

    二分答案.check有多少个区间的平均数>xbi=ai-x;将sm离散化.然后logn求出有多少个小于sm[i].类似于求逆序对的思路. 一直WA一个点...所以我就下载数据特判了TAT #in ...

  2. mysql-主从复制(一)

    1)用户授权 grant all privileges on share.* to 'abc'@'192.168.1.105' identified by '123456' 2)开启mysql的bin ...

  3. BOM浏览器对象

    BOM 浏览器对象 一.浏览器本身就自己有一些对象,不用创建就可以使用 window(当前浏览器窗体) 属性: status opener closed parent top 方法: alert(); ...

  4. ecshop init.php文件分析(转)

    <?php /** * ECSHOP 前台公用文件 */ //防止非法调用 defined-判断常量是否已定义,如果没返回false if (!defined('IN_ECS')) { die( ...

  5. 【英语】Bingo口语笔记(61) - mind系列

  6. 如何自定义一个优雅的ContentProvider

    最近在code review的时候发现很多人的provider定义的不是很好,写的很粗糙 以至于代码健壮性不够好,可读性也不强 但是你既然写了content provider 就是要给别人调用的,如果 ...

  7. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:4.安装Oracle RAC FAQ-4.4.无法图形化安装Grid Infrastructure

    无法图形化安装: [grid@linuxrac1 grid]$ ./runInstaller Starting Oracle Universal Installer... Checking Temp ...

  8. php.ini中Magic_Quotes_Gpc开关设置

    如果你网站空间的php.ini文件里的magic_quotes_gpc设成了off,那么PHP就不会在敏感字符前加上反斜杠(\\),由于表单提交的内容可能含有敏感字符,如单引号('),就导致了SQL ...

  9. 多数据源问题--Spring+Ibatis 访问多个数据源(非分布式事务)

    有的时候,我在一个工程中需要访问两个以上的数据源,尤其是在系统集成的时候,以下是我在系统集成的时候遇到的情况,我的工程的架构是:spring2.0+ibatis2.0+struts1.2. 数据库是o ...

  10. linux 如何让程序在开机时启动,关机前关闭

    可以将自己所写的script的文件名写入/etc/rc.d/rc.local(用户自定义开机启动程序) 中,在/etc/rc.d/init.d 中貌似也可以