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 ;
}

最新文章

  1. MyEclipse打开 HTML 报错Failed to create the part&#39;s controls
  2. CSS 透明度 设置 兼容IE FF
  3. SpringBean
  4. Sql server之路 (三)添加本地数据库SDF文件
  5. python 实现求和、计数、最大最小值、平均值、中位数、标准偏差、百分比。
  6. Careercup - Google面试题 - 6332750214725632
  7. CMake实践(4)
  8. WiFi相关知识
  9. C# String.Format大全
  10. HDU2196 - Computer(树形DP)
  11. Node.js V0.12新特性之性能优化
  12. jdbc接口api
  13. Contains Duplicate II 解答
  14. asp.net从客户端检测到有潜在危险的Request.Form 值
  15. Collections.sort的两种用法
  16. elasticsearch聚合查询
  17. AFNetworking 源码解读
  18. python3 读取文件跳过文件第一行内容
  19. Spring学习(十)-----Spring依赖检查
  20. day-20 tensorflow持久化之入门学习

热门文章

  1. P1982 小朋友的数字
  2. Android ToolBar标题文字居中的方法
  3. LightOJ 1422 Halloween Costumes 【 区间dp 】
  4. 【转】Oracle基础结构认知——oracle内存结构 礼记八目 2017-12-15 20:31:27
  5. 安装oracle执行runInstaller文件时报错:“……/install/.oui:Permission denied”
  6. centos7 redmine安装过程(转载)
  7. Javaee 方法的格式和注意事项
  8. Python数据结构1-----基本数据结构和collections系列
  9. Python数据分析1------数据存取
  10. IOS - 绘制文字 drawInRect: withFont: not working