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;
}
- 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 数组属性和方法
- Leetcode 第23场双周赛D 5363. 做菜顺序(DP,贪心)
- Leetcode 22. 括号生成 (括号匹配,dfs)
- MQ 系列之 ActiveMQ 介绍
- Python Tips(1) 数字与字符串之间转换,采用内置函数
- Spring Web MVC 拦截器
- Spring Web MVC 响应消息
- Codeforces Round #633 (Div. 2)D Edge Weight Assignment(构造、树的权值异或)
- Spring Web MVC 请求消息
- Codeforces Round #633 (Div. 2) A Filling Diamonds (假题,观察)
- 如何管理和组织一个机器学习项目
- Spring Web MVC 简单使用
- Spring 中的 JDBC
- IDEA 快键键:展开所有文件夹、折叠所有文件夹(自定义)
- mysql 数据库的悲观锁和乐观锁
- C语言 二维数组和指针的一些笔记