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+, 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 树状数组/二分的更多相关文章
- POJ 2182 Lost Cows 【树状数组+二分】
题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- TZOJ 4602 高桥和低桥(二分或树状数组+二分)
描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...
- 树状数组+二分||线段树 HDOJ 5493 Queue
题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...
- UVA 11423 - Cache Simulator (树状数组)
UVA 11423 - Cache Simulator (树状数组) option=com_onlinejudge&Itemid=8&category=523&page=sho ...
- P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]
题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...
- The Stream of Corning 2( 权值线段树/(树状数组+二分) )
题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...
- 牛客多校第3场 J 思维+树状数组+二分
牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...
- UVA 11610 Reverse Prime (数论+树状数组+二分,难题)
参考链接http://blog.csdn.net/acm_cxlove/article/details/8264290http://blog.csdn.net/w00w12l/article/deta ...
随机推荐
- win7如何给虚拟机设置共享文件
友情提示:设置之前先把虚拟机关掉 1. 安装vmtools 安装过的,则不需要 重新安装 如果没有安装vmware tools,点击安装(需要联网下载) ,下载完成后,打开虚拟机 点击安装,安装完毕后 ...
- 全面改造升级内部OA系统
项目功能集团的OA办公系统,分别是销售管理系统.财务付款系统.原料采购系统.成品采购系统.担保系统和库房管理系统业务现状成品采购系统.库房管理系统.销售管理系统是Access开发的C/S系统,采用本地 ...
- Android--XML页面的编写
五个页面 代码如下: 图片资源链接: https://pan.baidu.com/s/1jIoTDGE // 第一个 <RelativeLayout xmlns:andr ...
- VS 在代码中括号总是跟着类型后面
if (OK.Text.Contains("运费")) {// 像这样子,这个大括号不是直接在IF下,而是跟在后面 工具-->选项-->文本编辑器-->C# -- ...
- php生成唯一识别码uuid
/*生成唯一标志*标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx(8-4-4-4-12)*/ function uuid() { $chars = md ...
- C语言break/continue/exit/return的功能区别
break是跳出整个循环而执行循环体之外的下一条语句: continue只是跳出本次循环继续判断下一次循环条件是否满足. exit() 结束当前进程/当前程式/,在整个程式中,只要调用 exit ,就 ...
- URLLib库使用
Date: 2019-06-19 Author: Sun urllib 在Python 3以后的版本中,urllib2这个模块已经不单独存在(也就是说当你import urllib2时,系统提示你 ...
- RabbitMQ基础知识(转载)
RabbitMQ基础知识(转载) 一.背景 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需 ...
- mac 安装卸载python
第 1 步,删除框架: sudo rm -rf /Library/Frameworks/Python.framework/Versions/x.x第 2步,删除应用目录: sudo rm -rf &q ...
- C++基础 (3) 第三天 构造函数 构造函数初始化列表 拷贝构造函数 析构函数 静态成员变量
// 同类之间无私处 2构造函数 3析构函数 4构造函数的种类和析构函数的顺序 结论:析构函数的调用顺序,跟对象的构造顺序相反,谁先构造,谁最后一个被析构. 拷贝构造函数: 注意: 等号写在下面和写在 ...