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. PHP原生DOM对象操作XML的方法解答

    创建一个新的XML文件,并且写入一些数据到这个XML文件中. /** 创建xml文件*/ $info = array(array('obj' => 'power','info' => 'p ...

  2. 【JZOJ4929】【NOIP2017提高组模拟12.18】B

    题目描述 在两个n*m的网格上染色,每个网格中被染色的格子必须是一个四联通块(没有任何格子被染色也可以),四联通块是指所有染了色的格子可以通过网格的边联通,现在给出哪些格子在两个网格上都被染色了,保证 ...

  3. java读取项目路径下的中文文件乱码问题

    出现乱码错误: 处理方案: 对文件路径中存在中文的,都要进行URLDecoder.decode(path,"UTF-8")编码转换 wordContent = URLEncoder ...

  4. SQL执行计划Cost与性能之间的的关系

    关于执行计划Cost的三个疑问: 1. 执行计划的Cost越低,SQL就一定跑得越快吗?或者说Cost 和 执行时间成比例关系吗? 2. Oracle 默认产生的执行计划是Cost最低的吗? 3. 如 ...

  5. C# 获取上传文件的文件名和后缀名

    //获得要上传的文件 HttpPostedFile file = Request.Files[]; //获得到文件名 string fileName = System.IO.Path.GetFileN ...

  6. 寒哥教你学iOS - 经验漫谈

    http://www.jianshu.com/p/cb54054d3add 寒哥教你学iOS - 经验漫谈 字数2848 阅读1896 评论19 喜欢43 顺便来个广告 iOS开发者 群1734993 ...

  7. 《js高级程序设计》6.1.1-6.1.3——数据属性、访问器属性

    数据属性:该属性包含了一个数据值的位置,它包含了4个描述行为的特性:1. [[Configurable]]:表示是否能通过delete删除属性从而重新定义属性,能否修改属性的特性,能否把属性修改为访问 ...

  8. Ubuntu+Apache+PHP+Mysql环境搭建(完整版)(转)

    http://www.2cto.com/os/201505/401588.html Ubuntu+Apache+PHP+Mysql环境搭建(完整版) 一.操作系统Ubuntu 14.04 64位,阿里 ...

  9. python-selenium自动化测试(火狐、谷歌、360浏览器启动)

    一.打开谷歌浏览器 import selenium from selenium import webdriver browser = webdriver.Chrome(executable_path ...

  10. SP2-0642: SQL*Plus internal error state 2130, context 0:0:0

    ..experience, Working case SP2-0642: SQL*Plus internal error state 2130, context 0:0:0 2016-10-09 没有 ...