After the last war devastated your country, you - as the king of the land of Ardenia - decided it was
high time to improve the defense of your capital city. A part of your fortification is a line of mage
towers, starting near the city and continuing to the northern woods. Your advisors determined that the
quality of the defense depended only on one factor: the length of a longest contiguous tower sequence
of increasing heights. (They gave you a lengthy explanation, but the only thing you understood was
that it had something to do with firing energy bolts at enemy forces).
  After some hard negotiations, it appeared that building new towers is out of question. Mages of
Ardenia have agreed to demolish some of their towers, though. You may demolish arbitrary number of
towers, but the mages enforced one condition: these towers have to be consecutive.
  For example, if the heights of towers were, respectively, 5, 3, 4, 9, 2, 8, 6, 7, 1, then by demolishing
towers of heights 9, 2, and 8, the longest increasing sequence of consecutive towers is 3, 4, 6, 7.
Input
  The input contains several test cases. The first line of the input contains a positive integer Z ≤ 25,
denoting the number of test cases. Then Z test cases follow, each conforming to the format described
below.
  The input instance consists of two lines. The first one contains one positive integer n ≤ 2 · 105
denoting the number of towers. The second line contains n positive integers not larger than 109
separated by single spaces being the heights of the towers.
Output
  For each test case, your program has to write an output conforming to the format described below.
You should output one line containing the length of a longest increasing sequence of consecutive
towers, achievable by demolishing some consecutive towers or no tower at all.
Sample Input
2
9
5 3 4 9 2 8 6 7 1
7
1 2 3 10 4 5 6
Output
4
6

解题思路:

  本问题的关键在于set的动态更新,对set集合各种操作的熟练运用是关键。详细思路见紫书。

代码如下:

 #include <iostream>
#include <set>
#include <map>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=+;
int A[maxn];
int f[maxn];
int g[maxn];
int n;
map<int,int> m;
set<int> s; void pre_procession(){
int L=,R=;
while(L<n){
while(R<n&&(R==L||A[R]>A[R-])){g[R]=R+-L;R++;}
while(L<R){f[L]=R-L;L++;}
}
}
void putset(){
int ans=;
for(int i=;i<n;i++){
set<int>::iterator it=s.lower_bound(A[i]);
if(it!=s.begin()){
it--;
ans=max(ans,f[i]+m[*it]);
if(m[*it]>=g[i]) continue;
it++;
if(*it==A[i]&&m[*it]>=g[i]) continue;
}
s.insert(it, A[i]);
m[A[i]]=g[i];
int cur=A[i];
it=s.upper_bound(cur);
while(it!=s.end()&&m[cur]>=m[*it]){
cur=*it;
s.erase(it);
it=s.upper_bound(cur);
}
}
cout<<ans<<endl;
}
int main(int argc, const char * argv[]) {
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d",&A[i]);
pre_procession();
m.clear();
s.clear();
putset();
}
return ;
}

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 - 线段树 - 离散化

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

  3. uva 1471 Defense Lines

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

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

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

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

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

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

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

  7. Uva 1471 Defense Lines(LIS变形)

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

  8. 【二分】Defense Lines

    [UVa1471] Defense Lines 算法入门经典第8章8-8 (P242) 题目大意:将一个序列删去一个连续子序列,问最长的严格上升子序列 (N<=200000) 试题分析:算法1: ...

  9. UVa 1471 (LIS变形) Defense Lines

    题意: 给出一个序列,删掉它的一个连续子序列(该子序列可以为空),使得剩下的序列有最长的连续严格递增子序列. 分析: 这个可以看作lrj的<训练指南>P62中讲到的LIS的O(nlogn) ...

随机推荐

  1. JavaScript--预解析在IE存在的问题

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. oracle-12333错误

    Errors in file /oracle/OraHome1/admin/hndw/udump/hndw_ora_17941.trc: ORA-00600: internal error code, ...

  3. python控制台输出带颜色文字的方法

    目地:提高重要信息的可读性,方便用户阅读了. 书写格式如下: #格式: 设置颜色开始 :\033[显示方式;前景色;背景色m #说明: 前景色 背景色 颜色 --------------------- ...

  4. 杨柳目-杨柳科-Info-新闻:让中国人焦虑的杨絮背后,隐藏着“拯救”北京的秘密!

    ylbtech-杨柳目-杨柳科-Info-新闻:让中国人焦虑的杨絮背后,隐藏着“拯救”北京的秘密! 1.返回顶部 1. 春天来了,北京和其他很多城市满城飞絮的日子也到了.库叔作为敏感体质,不得不戴上口 ...

  5. JavaScript的六种数据类型与隐式转换

    一.六种数据类型 javascript的数据类型包括: (1)基本数据类型:number.string.boolean.null.undefined (2)对象:object object又包括Fun ...

  6. BZOJ 1834网络扩容题解

    一道不算太难的题目 但是真的很恶心 显然,对于第一问,我们直接无脑打模板就好了 第二问也不是很难,我们将每条边再连一条容量为inf,费用为w的边 但是流量只要小于第一问的答案加k就行了 所以我们增加一 ...

  7. (转)理解inode

    作者:阮一峰 原文链接:http://www.ruanyifeng.com/blog/2011/12/inode.html 一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上 ...

  8. Centos7环境下搭建Nginx+Lua+Redis进行数据存取

    1.安装依赖环境 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel 2.安装LuaJIT cd /usr/loca ...

  9. jquery的操作

    jQuery jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行 ...

  10. 最优化WPF 3D性能(基于“Tier-2”硬件)

    原文:最优化WPF 3D性能(基于"Tier-2"硬件) 原文地址:Maximizing WPF 3D Performance on Tier-2 Hardware 开发人员在应用 ...