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

题意:

  给你N个数的序列,可以删除一个连续子序列,使得剩下的序列中有一个长度最大的连续递增子序列,问你最长是多少

题解:

  我们首先想到的就是枚举 删除哪一段了,最后必定能出现最长

  但是明显是O(n^3)

  我们预处理对于 i 这个数向左向右分别能衍生的长度,这样是O(n^2),还不够

  我们只枚举右端点,不枚举左端点,而是用二分/树状数组找到一个j<i并且a[j]<a[i]的价值最大的a[j];

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N = 1e6+, M = , mod = 1e9+, inf = 0x3f3f3f3f;
typedef long long ll;
//不同为1,相同为0 int n,a[N],b[N],righ[N],lef[N],T;
int C[N],A[N];
int lowbit(int x) {
return x&(-x);
}
void add(int x, int add) {
for (; x < N; x += lowbit(x)) {
C[x] = max(add,C[x]);
}
}
int sum(int x) {
int s = ;
for (; x > ; x -= lowbit(x)) {
s = max(C[x],s);
}
return s;
}
void init() {
memset(C,,sizeof(C));
memset(righ,,sizeof(righ));
memset(lef,,sizeof(lef));
righ[n] = ;lef[] = ;
for(int i = n-;i >= ;i--){
if(a[i] >= a[i+]) righ[i] = ;
else righ[i] = righ[i+] + ;
}
for(int i = ;i <= n;i++){
if(a[i] > a[i-]) lef[i] = lef[i-] + ;
else lef[i] = ;
}
// for(int i=n;i>=1;i--) cout<<righ[i]<<endl;
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
int ans = ;
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]),b[i] = a[i];
sort(b+,b+n+);
int c = unique(b+,b+n+) - b - ;
for(int i=;i<=n;i++) a[i] = lower_bound(b+,b+c+,a[i]) - b;
init();
add(a[],lef[]);
for(int i=;i<=n;i++) {
ans = max(ans,sum(a[i]-)+righ[i]);
add(a[i],lef[i]);
}
printf("%d\n",ans);
}
return ;
}

UVA - 1471 Defense Lines 树状数组/二分的更多相关文章

  1. POJ 2182 Lost Cows 【树状数组+二分】

    题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  2. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  3. TZOJ 4602 高桥和低桥(二分或树状数组+二分)

    描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...

  4. 树状数组+二分||线段树 HDOJ 5493 Queue

    题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...

  5. UVA 11423 - Cache Simulator (树状数组)

    UVA 11423 - Cache Simulator (树状数组) option=com_onlinejudge&Itemid=8&category=523&page=sho ...

  6. P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]

    题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...

  7. The Stream of Corning 2( 权值线段树/(树状数组+二分) )

    题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...

  8. 牛客多校第3场 J 思维+树状数组+二分

    牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...

  9. UVA 11610 Reverse Prime (数论+树状数组+二分,难题)

    参考链接http://blog.csdn.net/acm_cxlove/article/details/8264290http://blog.csdn.net/w00w12l/article/deta ...

随机推荐

  1. 【TYVJ1936】【Clover杯NOIP模拟赛IV】太空战队

    [问题描述] 在社会经济高速发展的今天,借助高科技手段,组建太空战队的愿望就快实现了. 战队属下有N个航天员.作为空军选拔上来的佼佼者,每个航天员都有与生俱来的骄傲——他们每个人都认为自己是最强的或者 ...

  2. 谈谈cookie的弊端

    一.cookie虽然在数据在客户端持久存储提供了方便,但是分担了服务器数据传输的负担,还是存在很大的局限性的. 局限性: (1)在特定的域名下最多存储20个cookie. 浏览器版本          ...

  3. Vue.js和Nodejs的关系

    首先vue.js 是库,不是框架,不是框架,不是框架. Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据. Vue.js 的核心是一个允许你 ...

  4. Dvwa安装,配置(Linux)

    文章演示使用系统:CenTOS7 一:搭建LAMP环境 使用XAMPP安装部署,下载地址:https://www.apachefriends.org/download.html 1.1:赋予账号对XA ...

  5. linux 添加 msyql 开机自启动

    1.将服务文件拷贝到init.d下,并重命名为mysql cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld 2.赋予可 ...

  6. Android源代码分支、版本号、支持设备列表

    Build Branch Version Supported devicesOPD3.170816.023 android-8.0.0_r34 Oreo Pixel 2 XL, Pixel 2OPD1 ...

  7. centos6.6--------正向DNS配置

    一.正向区: 将域名解析为IP====================================================================================注 ...

  8. MIME类型记录

    Content-Disposition: attachment; filename="filename.xls" 提供下载

  9. MVC 入口

    1.在 Global.asax public class MvcApplication : System.Web.HttpApplication { protected void Applicatio ...

  10. Kafka学习笔记(1)----Kafka的简介和Linux下单机安装

    1. Kafka简介 Kafka is a distributed,partitioned,replicated commit logservice.它提供了类似于JMS的特性,但是在设计实现上完全不 ...