1471 - Defense Lines
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的更多相关文章
- UVA - 1471 Defense Lines 树状数组/二分
Defense Lines After the last war devastated your country, you - as the ...
- UVa 1471 Defense Lines - 线段树 - 离散化
题意是说给一个序列,删掉其中一段连续的子序列(貌似可以为空),使得新的序列中最长的连续递增子序列最长. 网上似乎最多的做法是二分查找优化,然而不会,只会值域线段树和离散化... 先预处理出所有的点所能 ...
- uva 1471 Defense Lines
题意: 给一个长度为n(n <= 200000) 的序列,你删除一段连续的子序列,使得剩下的序列拼接起来,有一个最长的连续递增子序列 分析: 就是最长上升子序列的变形.需要加一个类似二分搜索就好 ...
- UVA - 1471 Defense Lines (set/bit/lis)
紫薯例题+1. 题意:给你一个长度为n(n<=200000)的序列a[n],求删除一个连续子序列后的可能的最长连续上升子序列的长度. 首先对序列进行分段,每一段连续的子序列的元素递增,设L[i] ...
- 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[ ...
- 【二分】Defense Lines
[UVa1471] Defense Lines 算法入门经典第8章8-8 (P242) 题目大意:将一个序列删去一个连续子序列,问最长的严格上升子序列 (N<=200000) 试题分析:算法1: ...
- UVa 1471 (LIS变形) Defense Lines
题意: 给出一个序列,删掉它的一个连续子序列(该子序列可以为空),使得剩下的序列有最长的连续严格递增子序列. 分析: 这个可以看作lrj的<训练指南>P62中讲到的LIS的O(nlogn) ...
随机推荐
- C++11中的并发
在 C++98 的时代,C++标准并没有包含多线程的支持,人们只能直接调用操作系统提供的 SDK API 来编写多线程程序,不同的操作系统提供的 SDK API 以及线程控制能力不尽相同.到了 C++ ...
- 如何建一个Liferay 7的theme
首先附上原文链接Creating theme and Deploying in liferay 7 by using Eclipse 1.第一步:建一个Liferay module 项目,选择them ...
- PSPP:顶替SPSS常用功能的优秀软件, Linux 下的经济学用软件
几个替代SPSS的软体Salstat http://salstat.sourceforge.net/PSPP http://www.gnu.org/software/pspp/pspp.htmlR h ...
- Linux与Unix shell编程指南(完整高清版).pdf
找到一本很详细的Linux Shell脚本教程,其实里面不光讲了Shell脚本编程,还介绍了系统的各种命令 http://vdisk.weibo.com/s/yVBlEojGMQMpv 本书共分五部分 ...
- Java8中的LocalDateTime工具类
网上搜索了半天都没有找到Java8的LocalDateTime的工具类,只好自己写了一个,常用功能基本都有.还在用Date的Java同道该换换了. 个人项目地址:https://github.com/ ...
- Directx教程(28) 简单的光照模型(7)
原文:Directx教程(28) 简单的光照模型(7) 现实生活中的点光源都是随着距离衰减的,比如一个电灯泡在近处会照的很亮,远处光线就很弱.本节中我们在前面光公式的基础上,再给漫反射和 ...
- 简单的requestAnimationFrame动画
html部分 <div id="test" style="width:1px;height:17px;background:#0f0;">0%< ...
- hdu 1728 逃离迷宫 BFS加优先队列 DFS()
http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意就是能否在规定的转弯次数内从起点走到终点.刚走时那步方向不算. 只会bfs(),但想到这题需要记录转弯 ...
- 部署zabbix3.2.7,升级到3.4、proxy部署
一.Server安装 可以使用mysql.percona.mariadb等,本例使用mariadb.由于是事后整理,只截取部分命令. #yum install mariadb-server maria ...
- zabbix源码编译安装以及添加第一台host监控
基础准备 硬件需求 数据库需求 软件需求 其他软件需求 安装 安装方式 source code 编译好的二进制包 rpm或者deb 源码编译安装部署zabbix以及附件 前提准备 最小化安装操作系 ...