题意是说给一个序列,删掉其中一段连续的子序列(貌似可以为空),使得新的序列中最长的连续递增子序列最长。

  网上似乎最多的做法是二分查找优化,然而不会,只会值域线段树和离散化。。。

  先预处理出所有的点所能延伸到最左端的长度,和到最右端的长度,然后离散化,然后对于当前的点,就交给值域线段树去查出前面最大的符合条件的向左延伸的长度,加上当前位置最大向右延伸的长度,更新答案即可。

Code

 /**
* UVa
* Problem#1471
* Accepted
* Time:1190ms
*/
#include<iostream>
#include<cstdio>
#include<cctype>
#include<ctime>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef bool boolean;
#define inf 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-');
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
} typedef class SegTreeNode {
public:
int val;
SegTreeNode *l, *r;
SegTreeNode(int val = , SegTreeNode* l = NULL, SegTreeNode* r = NULL):val(val), l(l), r(r) { } inline void pushUp() {
val = max(l->val, r->val);
}
}SegTreeNode; typedef class SegTree {
public:
SegTreeNode* root;
SegTree():root(NULL) { }
SegTree(int s) {
build(root, , s);
} void build(SegTreeNode*& node, int l, int r) {
node = new SegTreeNode();
if(l == r) return;
int mid = (l + r) >> ;
build(node->l, l, mid);
build(node->r, mid + , r);
} void update(SegTreeNode*& node, int l, int r, int idx, int val) {
if(l == idx && r == idx) {
smax(node->val, val);
return;
}
int mid = (l + r) >> ;
if(idx <= mid) update(node->l, l, mid, idx, val);
else update(node->r, mid + , r, idx, val);
node->pushUp();
} int query(SegTreeNode*& node, int l, int r, int ql, int qr) {
if(qr < ql) return -inf;
if(l == ql && r == qr) {
return node->val;
}
int mid = (l + r) >> ;
if(qr <= mid) return query(node->l, l, mid, ql, qr);
if(ql > mid) return query(node->r, mid - , r, ql, qr);
int sl = query(node->l, l, mid, ql, mid);
int sr = query(node->r, mid + , r, mid + , qr);
return max(sl, sr);
} inline void clear(SegTreeNode*& node) {
if(node == NULL) return;
clear(node->l);
clear(node->r);
delete[] node;
}
}SegTree; int T;
int n;
int len;
int* lis;
int* buf;
SegTree st; inline void init() {
readInteger(n);
lis = new int[(const int)(n + )];
buf = new int[(const int)(n + )];
for(int i = ; i <= n; i++)
readInteger(lis[i]);
} inline void descreate(int* a) {
memcpy(buf, a, sizeof(int) * (n + ));
sort(buf + , buf + n + );
len = unique(buf + , buf + n + ) - buf;
for(int i = ; i <= n; i++)
a[i] = lower_bound(buf + , buf + len, a[i]) - buf;
} int *tol, *tor;
inline void dp() {
tol = new int[(const int)(n + )];
tor = new int[(const int)(n + )];
tol[] = ;
for(int i = ; i <= n; i++)
tol[i] = (lis[i] > lis[i - ]) ? (tol[i - ] + ) : ();
tor[n] = ;
for(int i = n - ; i > ; i--)
tor[i] = (lis[i] < lis[i + ]) ? (tor[i + ] + ) : ();
} int result;
inline void solve() {
result = ;
descreate(lis);
st = SegTree(len);
dp();
// st.update(st.root, 1, len, lis[1], tol[1]);
for(int i = ; i <= n; i++) {
int c = st.query(st.root, , len, , lis[i] - );
smax(result, c + tor[i]);
st.update(st.root, , len, lis[i], tol[i]);
}
printf("%d\n", result);
delete[] tol;
delete[] tor;
delete[] buf;
delete[] lis;
st.clear(st.root);
} int main() {
readInteger(T);
while(T--) {
init();
solve();
}
return ;
}

UVa 1471 Defense Lines - 线段树 - 离散化的更多相关文章

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

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

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

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

  3. uva 1471 Defense Lines

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

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

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

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

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

  6. Uva 1471 Defense Lines(LIS变形)

    题意: 给你一个数组,让你删除一个连续的子序列,使得剩下的序列中有最长上升子序列, 求出这个长度. 题解: 预处理:先求一个last[i],以a[i]为开始的合法最长上升子序列的长度.再求一个pre[ ...

  7. HDU5124:lines(线段树+离散化)或(离散化思想)

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...

  8. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  9. D - Mayor's posters(线段树+离散化)

    题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...

随机推荐

  1. MySQL半同步复制(5.5之后引入)

    半同步复制架构在主库提交一个事务后,commit完成即反馈客户端,无需等待推送binlog完成,如图: 半同步复制在主库完成一个事务后,需等待事务信息写入binlog日志并且至少有一个从库写入rela ...

  2. SQL中的四种语言DDL,DML,DCL,TCL

    1.DDL(Data Definition Language)数据库定义语言statements are used to define the database structure or schema ...

  3. Net Promoter Score

    https://baike.baidu.com/item/净推荐值/3783368?fr=aladdin NPS(Net Promoter Score),净推荐值,又称净促进者得分,亦可称口碑,是一种 ...

  4. win7 链接打印机时提示未知的用户名或错误密码

    使用win7系统时,访问局域网内的计算机 \\ip 要求输入正确用户名和密码,输入用户名和密码后,还是一直提示“未知的用户名或错误密码”. 解决方法: 开始---->运行 打开组策略编辑器 gp ...

  5. HDU5033 building 单调栈+计算几何

    正解:单调栈 解题报告: 哇生气辽QAQ本来打了半天feel good都快调出来了然后说换题了QAQ(所以可能那题的代码会过一阵子再放上来了QAQ 不过还是大爆手速打了一通拿到首杀了嘻嘻 美滋滋辽 然 ...

  6. 从jvm来看,scala中的@究竟是个什么鬼?@模式匹配符号(scala 词法分析 语法分析常用)

    从jvm来看,scala中的@究竟是个什么鬼? 我也是初步尝试来看jvm的类文件,又是初次来分析@,如不对的地方,请各位指正! 先看一下@ 是个什么? object TestScala { def m ...

  7. (转)ElasticSearch Java Api-检索索引库

    上篇博客记录了如何用java调用api把数据写入索引,这次记录下如何搜索. 一.准备数据 String data1 = JsonUtil.model2Json(new Blog(1, "gi ...

  8. ab压力测试遭遇apr_socket_recv: Connection reset by peer (104) 怎么办

    ab -r  -c 2000 -n 200000  www.baidu.com 其实只要加上-r就可以了.但是为什么呢?ab --help就知道了 当Socket接收到错误的时候不退出,就是这句.事实 ...

  9. 支持向量机(SVM)、支持向量回归(SVR)

    1.支持向量机( SVM )是一种比较好的实现了结构风险最小化思想的方法.它的机器学习策略是结构风险最小化原则 为了最小化期望风险,应同时最小化经验风险和置信范围) 支持向量机方法的基本思想: ( 1 ...

  10. selenium webdriver模拟鼠标键盘操作

    在测试使用Selenium webdriver测试WEB系统的时候,用到了模拟鼠标.键盘的一些输入操作. 1.鼠标的左键点击.双击.拖拽.右键点击等: 2.键盘的回车.回退.空格.ctrl.alt.s ...