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 ...
随机推荐
- tcpdump抓包二进制tcp协议详细分析
1.tcpdump -i eth0 port 11751 and src host 192.168.1.34 -x -s0 tcpdump: verbose output suppressed, us ...
- snowflake and uuid
分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的. 有些时候我们希望能使用一种简单一 ...
- Jmeter(十五)_上传与下载
今天重点说一下Jmeter的上传与下载 1:Jmeter上传文件 首先确认你的文件名称,参数名称,MIME类型,这些可以从接口文档里面获取,或者直接在页面抓包然后从请求头里面查看.注意,此处我的文件路 ...
- Jena 操作 RDF 文件
1. RDF 入门 RDF(Resource Description Framework)是由W3C规定的,描述资源(resource)的数据模型(data model),: RDF 使用Web标识符 ...
- 青蛙的约会---poj1061(扩展欧几里德)
题目链接:http://poj.org/problem?id=1061 就是找到满足 (X+mt)-(Y+nt) = Lk 的 t 和 k 即可 上式可化简为 (n-m)t + Lk = X-Y;满足 ...
- 15款Django开发常用软件包(转)
原文:http://www.iteye.com/news/28697 Django是一款高级的Python Web框架,可以帮助开发者快速创建web应用.我们这里整理了15款Django开发中常用的软 ...
- linux中fork函数详解(转)
add by zhj: 在Linux,创建进程是用fork(),它其实就是拷贝父进程的数据段和其它数据,这相当于C函数调用中的值传递,这是 此后两者的修改都互不影响.因为两者的数据虽相同,但却在不同的 ...
- 聊一聊Linux中的工作队列2
上一篇文章对工作队列原理以及核心数据结构做了简单介绍,本文重点介绍下workqueue的创建以及worker的管理. 一.工作队列的创建(__alloc_workqueue_key) struct w ...
- ansible 批量在远程主机上执行命令
ansible 和 saltstack 都是为了同时在多台主机上执行相同的命令, 但是 salt配置麻烦,ansible基本不用配置, ansible 通过ssh来连接并控制被控节点 1. 安装 第一 ...
- Dom最常用的API
document方法: getElementById(id) Node 返回指定结点的引用 getElementsByTagName(name) NodeList 返回文档中所有匹配的元素的集合 cr ...