Codeforces Round #605 (Div. 3) D. Remove One Element
D. Remove One Element
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array aa consisting of nn integers.
You can remove at most one element from this array. Thus, the final length of the array is n−1n−1 or nn.
Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array.
Recall that the contiguous subarray aa with indices from ll to rr is a[l…r]=al,al+1,…,ara[l…r]=al,al+1,…,ar. The subarray a[l…r]a[l…r] is called strictly increasing if al<al+1<⋯<aral<al+1<⋯<ar.
Input
The first line of the input contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of elements in aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is the ii-th element of aa.
Output
Print one integer — the maximum possible length of the strictly increasing contiguous subarray of the array aa after removing at most one element.
Examples
input
Copy
5
1 2 5 3 4
output
Copy
4
input
Copy
2
1 2
output
Copy
2
input
Copy
7
6 5 4 3 2 4 3
output
Copy
2
Note
In the first example, you can delete a3=5a3=5. Then the resulting array will be equal to [1,2,3,4][1,2,3,4] and the length of its largest increasing subarray will be equal to 44.
哎,这题真的值得学习一下,dp运用的非常灵活,其实观察题目可知,最长连续递增序列长度增加只可能是两个小段的合并观察到这一点很重要!
没有删去元素时,运用dp,我们可以求出整个序列的最长递增序列长度,f[i]代表以元素a[i]结尾的序列的最长递增序列长度,递推方程如下:
f[i+1]=f[i]+1 if(a[i]<a[i+1)
else f[i+1]=1
有删去元素时,我们接着这样考虑:对于答案唯一能够改变的可能就是删掉了这个元素之后,前后两个递增序列合并起来变的大了,答案更新了吗?
所以,比方说我们要删除第i个元素,if(a[i-1]<a[i+1])那么可能会更新答案,那不就是f[i-1]加上一个以a[i+1]元素开头的最长递增子序列的长度吗?
那这个怎么求?还是o(n)dp啊!
ok,已AC~,细节详见代码
#include<bits/stdc++.h>
#define ll long long
#define rg register ll
using namespace std;
#define inf 2147483647
ll a[200005],f[200005],g[200005],maxx=-inf;
int main()
{
ll n;
cin>>n;
for(rg i=1;i<=n;i++)cin>>a[i];
f[1]=1,g[n]=1;
for(rg i=2;i<=n;i++)
{
if(a[i]>a[i-1])f[i]=f[i-1]+1;
else f[i]=1;
}
for(rg i=n-1;i>=1;i--)
{
if(a[i]<a[i+1])g[i]=g[i+1]+1;
else g[i]=1;
}
for(rg i=1;i<=n;i++)maxx=max(maxx,f[i]);
for(rg i=1;i<=n;i++)
{
if(a[i-1]<a[i+1])maxx=max(maxx,f[i-1]+g[i+1]);
}
cout<<maxx<<endl;
while(1)getchar();
return 0;
}
- Apache Ignite——新一代数据库缓存系统
- 微信JSSDK接入Java版--步骤及问题处理和解决
- 微信企业号回调模式配置讲解 Java Servlet+Struts2版本 echostr校验失败解决
- Android Material Design系列之RecyclerView和CardView
- 在Linux安装ASP.Net Core的运行时(Runtime)
- 使用xUnit为.net core程序进行单元测试(下1)
- Otto开发初探——微服务依赖管理新利器
- Apache Eagle——eBay开源分布式实时Hadoop数据安全方案
- Spring/Hibernate 应用性能优化的7种方法
- 浅谈应用型机器学习作为一种搜索问题
- 自相关和偏自相关的简单介绍
- 机器学习中分类与回归的差异
- 自然语言处理指南(第1部分)
- GreenDao 兼容升级,保留旧数据的---全方面解决方案
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 虚拟机设置桥接上网(下)
- WebSth 指纹识别插件简要分析
- Android实现3D层叠式卡片图片展示
- Android Studio屏幕方向以及UI界面状态的保存代码详解
- webshell,禁止你执行
- android实现下拉菜单三级联动
- Flutter 假异步的实现示例
- 详解Android4.4 RIL短信接收流程分析
- android 使用kotlin 实现点击更换全局语言(中日英切换)
- EasySec基于XP的渗透平台
- Android集成腾讯X5实现文档浏览功能
- 8个小时8个小时的上机课!!
- Android实现根据评分添加星级条
- Android悬浮窗的实现(易错点)
- phpcms上传导致getshell详解及案例