题意:

  给你一个数组,让你删除一个连续的子序列,使得剩下的序列中有最长上升子序列, 求出这个长度。

题解:

  预处理:先求一个last[i],以a[i]为开始的合法最长上升子序列的长度。再求一个pre[i],以a[i]为结尾的合法最长上升子序列长度。

  那么这题的答案就是:max(pre[j]) + last[i]。(j<=a[i] - 1)。

  例如a[i]为3的话, 答案就是max(以1结尾的LIS长度, 以2结尾的LIS长度) + 以3开始LIS长度。

  dis[i] 是 LIS 长度为i的时候,最小的a[i]。(详细请看LIS nlogn算法)

  所以

  len = (lower_bound(dis+1, dis+1+i, a[i]) - (dis+1)) + last[i]。//二分查找

  ans = max (ans, len);

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
#define eps 0.0000001
typedef long long LL;
const int inf = 0x3f3f3f3f;
const LL INF = 0x7fffffff;
const int maxn = +;
const int mod = ;
int a[maxn];
int last[maxn];
int pre[maxn];
int dis[maxn];
void solve()
{
int n;
scanf("%d", &n);
for(int i=;i<=n;i++) scanf("%d", &a[i]);
ms(pre, );
ms(last, );
last[n] = ;
for(int i=n-;i>;i--){
if(a[i] < a[i+]){
last[i] = last[i+] + ;
}else last[i] = ;
}
pre[] = ;
for(int i= ;i<=n;i++){
if(a[i] > a[i-]){
pre[i] = pre[i-] + ;
}else pre[i] = ;
}
for(int i=;i<=n;i++) dis[i] = inf;
int ans = ;
for(int i=;i<=n;i++){
int len = (lower_bound(dis+, dis++i, a[i]) -(dis+))+ last[i];
ans = max(ans, len);
dis[pre[i]] = min(a[i], dis[pre[i]]);
}
printf("%d\n", ans);
}
int main() {
#ifdef LOCAL
freopen("jumping.in", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL
int t;
scanf("%d", &t);
while(t--){
solve();
}
return ;
}

Uva 1471 Defense Lines(LIS变形)的更多相关文章

  1. UVA - 1471 Defense Lines 树状数组/二分

                                  Defense Lines After the last war devastated your country, you - as the ...

  2. UVA 1471 Defense Lines 防线 (LIS变形)

    给一个长度为n的序列,要求删除一个连续子序列,使剩下的序列有一个长度最大的连续递增子序列. 最简单的想法是枚举起点j和终点i,然后数一数,分别向前或向后能延伸的最长长度,记为g(i)和f(i).可以先 ...

  3. UVA - 1471 Defense Lines (set/bit/lis)

    紫薯例题+1. 题意:给你一个长度为n(n<=200000)的序列a[n],求删除一个连续子序列后的可能的最长连续上升子序列的长度. 首先对序列进行分段,每一段连续的子序列的元素递增,设L[i] ...

  4. UVa 1471 Defense Lines - 线段树 - 离散化

    题意是说给一个序列,删掉其中一段连续的子序列(貌似可以为空),使得新的序列中最长的连续递增子序列最长. 网上似乎最多的做法是二分查找优化,然而不会,只会值域线段树和离散化... 先预处理出所有的点所能 ...

  5. uva 1471 Defense Lines

    题意: 给一个长度为n(n <= 200000) 的序列,你删除一段连续的子序列,使得剩下的序列拼接起来,有一个最长的连续递增子序列 分析: 就是最长上升子序列的变形.需要加一个类似二分搜索就好 ...

  6. UVa 1471 Defense Lines (二分+set优化)

    题意:给定一个序列,然后让你删除一段连续的序列,使得剩下的序列中连续递增子序列最长. 析:如果暴力枚举那么时间复杂度肯定受不了,我们可以先进行预处理,f[i] 表示以 i 结尾的连续最长序列,g[i] ...

  7. uva 1471 defence lines——yhx

    After the last war devastated your country, you - as the king of the land of Ardenia - decided it wa ...

  8. 1471 - Defense Lines

    After the last war devastated your country, you - as the king of the land of Ardenia - decided it wa ...

  9. UVA 437 巴比伦塔 【DAG上DP/LIS变形】

    [链接]:https://cn.vjudge.net/problem/UVA-437 [题意]:给你n个立方体,让你以长宽为底,一个个搭起来(下面的立方体的长和宽必须大于上面的长和宽)求能得到的最长高 ...

随机推荐

  1. 【ABAP系列】SAP LSMW(摘自官网)

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP LSMW(摘自官网)   前 ...

  2. LeetCode——28. Implement strStr()

    题目: class Solution { public: int strStr(string haystack, string needle) { if(needle.empty()){ return ...

  3. Echars -- 在Vue中如何使用Echars

    在vue-cli项目中使用echarts -->Wangqi   这个示例使用 vue-cli 脚手架搭建 安装echarts依赖 npm install echarts -S 或者使用国内的淘 ...

  4. Git利用命令行提交代码步骤

    利用命令行提交代码步骤进入你的项目目录1:拉取服务器代码,避免覆盖他人代码git pull2:查看当前项目中有哪些文件被修改过git status具体状态如下:1:Untracked: 未跟踪,一般为 ...

  5. python 字符串 string模块导入及用法

    字符串也是一个模块,有自己的方法,可以通过模块导入的方式来调用 1,string模块导入 import string 2,  其用法 string.ascii_lowercase string.dig ...

  6. 回溯---Permutations II

    47.Permutations II (Medium)](https://leetcode.com/problems/permutations-ii/description/) [1,1,2] hav ...

  7. CSS中如何设置父元素透明度不影响子元素透明度

    原因分析: 使用css的opcity属性改变某个元素的透明度,但是其元素下的子元素的透明度也会被改变,即便重定义也没有用,不过有个方法可以实现,大家可以看看. 可以使用一张透明的图片做背景可以达成效果 ...

  8. Conference - open source drives IOT from device to edge

    Open source drives IOT from device to edge 以下都是针对IOT领域的项目: ACRN A Big Little Hypervisor for IoT Deve ...

  9. VMware Workstation安装CentOs7固定ip地址

    今天发现之前hypervisor配置的CentOs7连接不了了,该死的加密系统和杀毒软件又搞事情了,于是决定试下VMware虚拟机,下载安装后,发现可以连上CentOS7界面,很开心,于是决定把之前的 ...

  10. 获取用户真实IP:(模拟:客户端--F5--nginx--tomcat 后端获取用户真实IP)

    模拟:客户端--F5--nginx--tomcat 后端获取用户真实IP 192.168.109.137 :nginx01(充当第一层代理==F5)192.168.109.138 :nginx02(二 ...