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

题意是说给一个序列,删掉其中一段连续的子序列(貌似可以为空),使得新的序列中最长的连续递增子序列最长。
网上似乎最多的做法是二分查找优化,然而不会,只会值域线段树和离散化。。。
先预处理出所有的点所能延伸到最左端的长度,和到最右端的长度,然后离散化,然后对于当前的点,就交给值域线段树去查出前面最大的符合条件的向左延伸的长度,加上当前位置最大向右延伸的长度,更新答案即可。
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 - 线段树 - 离散化的更多相关文章
- UVA - 1471 Defense Lines 树状数组/二分
Defense Lines After the last war devastated your country, you - as the ...
- UVA - 1471 Defense Lines (set/bit/lis)
紫薯例题+1. 题意:给你一个长度为n(n<=200000)的序列a[n],求删除一个连续子序列后的可能的最长连续上升子序列的长度. 首先对序列进行分段,每一段连续的子序列的元素递增,设L[i] ...
- uva 1471 Defense Lines
题意: 给一个长度为n(n <= 200000) 的序列,你删除一段连续的子序列,使得剩下的序列拼接起来,有一个最长的连续递增子序列 分析: 就是最长上升子序列的变形.需要加一个类似二分搜索就好 ...
- UVA 1471 Defense Lines 防线 (LIS变形)
给一个长度为n的序列,要求删除一个连续子序列,使剩下的序列有一个长度最大的连续递增子序列. 最简单的想法是枚举起点j和终点i,然后数一数,分别向前或向后能延伸的最长长度,记为g(i)和f(i).可以先 ...
- UVa 1471 Defense Lines (二分+set优化)
题意:给定一个序列,然后让你删除一段连续的序列,使得剩下的序列中连续递增子序列最长. 析:如果暴力枚举那么时间复杂度肯定受不了,我们可以先进行预处理,f[i] 表示以 i 结尾的连续最长序列,g[i] ...
- Uva 1471 Defense Lines(LIS变形)
题意: 给你一个数组,让你删除一个连续的子序列,使得剩下的序列中有最长上升子序列, 求出这个长度. 题解: 预处理:先求一个last[i],以a[i]为开始的合法最长上升子序列的长度.再求一个pre[ ...
- HDU5124:lines(线段树+离散化)或(离散化思想)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- D - Mayor's posters(线段树+离散化)
题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...
随机推荐
- 【紫书】 Unix ls UVA - 400 模拟
题意:中文版https://vjudge.net/problem/UVA-400#author=Zsc1615925460 题解:首先读取字符,维护一个最长字符串长度M,再排序. 对于输出,写一个pr ...
- Python面向对象:继承和多态
继承与多态简介: 继承可以把父类的所有功能都直接拿过来,这样就不必重零做起,子类只需要新增自己特有的方法,也可以把父类不适合的方法覆盖重写. 动态语言的鸭子类型特点决定了继承不像静态语言那样是必须的. ...
- Python:正则表达式概念
#正则表达式内容非常多,网上的学习资源也是目不暇接,我从中筛选学习并且整理出以下 的学习笔记 一.正则表达式匹配过程: 1.依次拿出表达式和文本中的字符比较 2.如果每一个字符都能匹配,则匹配成功:一 ...
- compile time - run-time
php.net Class member variables are called "properties". You may also see them referred to ...
- Web终端SSH功能
http://www.laozuo.org/10703.html------ CentOS安装配置GateOne实现Web终端SSH功能
- Excel-字符串连接
使用函数concatenate()将多个字符连接起来
- css 计算属性 calc的使用
宽度等于父元素的宽度减去16像素 高度等于父元素的高度减去16像素 注意:100%和16px 与减号之间必须有空格,否则高度会失效!!!! .box{ width:calc(100% - 16px ...
- oracle(一)复习起航
住了三年的宿舍,前几天不得不搬走.也断了好几天网,所以顺手拿了本以前买的<oracle编程艺术>,感觉翻译的书就是有些地方读起来不通顺,好吃力. 还好以前有点oracle经验,不然真看不懂 ...
- C#静态类,静态构造函数,静态变量
静态变量位于栈上,它是一个全局变量,在编译期就已经生成. public class Cow public static int count; private int id; { id = ++coun ...
- Choose the best route(迪杰斯特拉)
通过做这题,发现了自己的问题很大,做题不是贴代码,而是要了解思想:这题考的是有一个起点的集合,求起点集合到一个终点的最短距离, 本来想用Floy的但一看map[1000][1000]超时,有向图,逆序 ...