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. First Day Python介绍

    前言:刚开通的博客,谢谢博客园平台,管理辛苦! Python介绍 Python是一门高级的.面向对象的.解释性.脚本语言. 高级语言:贴近开发者,对应底层语言,底层语言贴近机器:java.C#.php ...

  2. JS轮播图动态渲染四种方法

    一. 获取轮播图数据  ajax 二.根据数据动态渲染 (根据当前设备 屏幕宽度判断) 1. 准备数据 2. 把数据转换成html格式的字符串 动态创建元素 字符串拼接 模板引擎 框架方法 2.把字符 ...

  3. 认识React框架

    在大厂面试的时候被问会不会React框架几乎是必须的,可见React框架在现在前端市场的份额.所以说学习React框架的必要性. react框架起源于Facebook的内部项目,因为对市场上的Java ...

  4. python 下串口数据的读取,解析,和保存-

    #!/usr/bin/python # -*-coding: utf-8 -*- import serial import threading import binascii from datetim ...

  5. Hadoop MapReduce编程 API入门系列之wordcount版本5(九)

    这篇博客,给大家,体会不一样的版本编程. 代码 package zhouls.bigdata.myMapReduce.wordcount1; import java.io.IOException; i ...

  6. 百度地图api的简单应用

    百度地图api 获取经纬度(通过浏览器的) //获取经纬度 window.navigator.geolocation.getCurrentPosition(function(position) { a ...

  7. 使用Custom scrollbar(彩色滚动条)插件实现WordPress滚动条变色的方法

    1.在插件中心关键词搜索Custom scrollbar 2.按照说明操作就行 查看演示:sheji.xinlvtian.com

  8. dataAdapter

    public static class DataAdapter { /// <summary> /// DataRow转换成Hash对象 /// </summary> /// ...

  9. 熟悉VS2017 和Github 第二次作业

    GIT地址 https://github.com/Astone1213 GIT用户名  Astone1213 学号后五位  62114 博客地址 https://www.cnblogs.com/AsL ...

  10. mysql数据库重点监控

    1. QPS  每秒钟查询数量     查询总数/秒数 queries per seconds show global status like 'Question%' 2.TPS   每秒钟的事物数 ...