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

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

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

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. In MySQL, a zero number equals any string

    最近在做项目的过程中发现了一个问题 数据库表 test  有个字段是 target_id  int(11),这个字段可能为零 使用如下查询 select * from test where targe ...

  2. webpack初步了解

    webpack是一个打包工具,基于nodeJS Webpack 可以将多种静态资源 js.css.less 转换成一个静态文件,减少了页面的请求. 安装 Webpack 由于 npm 安装速度慢,本教 ...

  3. Spark Streaming性能优化: 如何在生产环境下应对流数据峰值巨变

    1.为什么引入Backpressure 默认情况下,Spark Streaming通过Receiver以生产者生产数据的速率接收数据,计算过程中会出现batch processing time > ...

  4. [django实践]投票app

    code: https://github.com/lannyMa/toupiao polls app介绍 这个例子来源于django官网,恰好2.x版本有中文版. https://docs.djang ...

  5. 前m大的数(哈希入门)&&sort

    http://acm.hdu.edu.cn/showproblem.php?pid=1280 普通方法(625ms) #include <stdio.h> #include <str ...

  6. GridView 点滴

    绑定数据时.在后台给GridView添加事件 protected void grd_RowDataBound(object sender, GridViewRowEventArgs e) { //当前 ...

  7. hihocoder博弈游戏·Nim游戏·三

    在这一次游戏中Alice和Bob决定在原来的Nim游戏上增加一条规则:每一次行动时,不仅可以选择一堆取走任意数量的石子(至少取1颗,至多取出这一堆剩下的所有石子),还可以选择将一堆石子分成两堆石子,但 ...

  8. Nuget的学习总结

    Nuget的学习总结 今天研究了一下nuget,发现nuget实在是太有用了,便写下了这篇博客,希望记录一下自己的学习历程,也希望技术圈的朋友看到之后,如果里面哪里写的不够好,可以给我些宝贵的意见,以 ...

  9. python练习题-简单方法判断三个数能否组成三角形

    python简单方法判断三个数能否组成三角形 #encoding=utf-8 import math while True: str=raw_input("please input thre ...

  10. 打造高可靠与高性能的React同构解决方案

    前言 随着React的兴起, 结合Node直出的性能优势和React的组件化,React同构已然成为趋势之一.享受技术福利的同时,直面技术挑战,在复杂场景下,挑战10倍以上极致的性能优化. 什么是同构 ...