UVA - 1471 Defense Lines 树状数组/二分
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+7, M = 30005, mod = 1e9+7, 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 = 0; for (; x > 0; x -= lowbit(x)) { s = max(C[x],s); } return s; } void init() { memset(C,0,sizeof(C)); memset(righ,0,sizeof(righ)); memset(lef,0,sizeof(lef)); righ[n] = 1;lef[1] = 1; for(int i = n-1;i >= 1;i--){ if(a[i] >= a[i+1]) righ[i] = 1; else righ[i] = righ[i+1] + 1; } for(int i = 2;i <= n;i++){ if(a[i] > a[i-1]) lef[i] = lef[i-1] + 1; else lef[i] = 1; } // for(int i=n;i>=1;i--) cout<<righ[i]<<endl; } int main() { int T; scanf("%d",&T); while(T--) { int ans = 1; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i] = a[i]; sort(b+1,b+n+1); int c = unique(b+1,b+n+1) - b - 1; for(int i=1;i<=n;i++) a[i] = lower_bound(b+1,b+c+1,a[i]) - b; init(); add(a[1],lef[1]); for(int i=2;i<=n;i++) { ans = max(ans,sum(a[i]-1)+righ[i]); add(a[i],lef[i]); } printf("%d\n",ans); } return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· 程序员常用高效实用工具推荐,办公效率提升利器!
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)