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. 利用阿里云加速Docker For Windows

    1.进入阿里云的容器镜像服务,找到镜像中心的镜像加速器. https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors 2.进入Docker ...

  2. js例子

    1.子菜单下拉 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  3. java异常处理和自定义异常利用try和catch让程序继续下去(回来自己再写个例子试运行下)

    注意:想在catch的参数里使用自定义的异常,则必须先将这个异常抛出才行.(throws是具有抛出异常的能力,并未抛出,throw new MyException是抛出异常,catch是捕获异常,只有 ...

  4. HBase编程 API入门系列之HTable pool(6)

    HTable是一个比较重的对此,比如加载配置文件,连接ZK,查询meta表等等,高并发的时候影响系统的性能,因此引入了“池”的概念. 引入“HBase里的连接池”的目的是: 为了更高的,提高程序的并发 ...

  5. Hadoop MapReduce编程 API入门系列之多个Job迭代式MapReduce运行(十二)

    推荐 MapReduce分析明星微博数据 http://git.oschina.net/ljc520313/codeexample/tree/master/bigdata/hadoop/mapredu ...

  6. java的重载总结

    1.不能以返回值的不同来重载方法,编译都不通过(只有参数类型或者参数个数不同才可以重载方法) 在Java语言中,要重载一个方法,除了要与原方法具有相同的简单名称外,还要求必须拥有一个与原方法不同的(不 ...

  7. Gitlab smtp 设置

    地址: https://blog.csdn.net/yongche_shi/article/details/78677163 # 腾讯云限制25发信端口,修改为465,并开启. https://doc ...

  8. Hashlib 用户名密码加密 2.0

    #!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018/7/10 0008 11:44# @Author : Anthony.Waa# @ ...

  9. ComboBoxEdit 添加键值

    ComboBoxEdit combo = new ComboBoxEdit(); var coll = combo.Properties.Items; coll.BeginUpdate(); try ...

  10. 【Oracle】创建角色

    任务:创建角色 1)创建角色sse_role,授予create session 权限 2)创建角色tblo_role,授予CREATE PROCEDURE, CREATE SEQUENCE, CREA ...