CF 1272 D. Remove One Element
2 seconds
256 megabytes
standard input
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.
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.
Print one integer — the maximum possible length of the strictly increasing contiguous subarray of the array aa after removing at most one element.
5
1 2 5 3 4
4
2
1 2
2
7
6 5 4 3 2 4 3
2
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.
这个题我自己的写法WA了,改来改去太复杂,所以后来直接借用学长的思路。
用学长的思路写这篇博客吧,学长原贴:https://www.cnblogs.com/xyq0220/p/12036109.html
O(n) 预处理出l[i]为i作为终点的最长严格递增子段,r[i]为i作为起始点的最长严格递增子段。
不删除一个元素,ans=max{l[i]},(1<=i<=n)。
删除一个元素,ans=max{l[i−1]+r[i+1]},(1<i<n&&a[i−1]<a[i+1])
1 #include<bits/stdc++.h>
2 #define fi first
3 #define se second
4 #define lson l,mid,p<<1
5 #define rson mid+1,r,p<<1|1
6 #define pb push_back
7 #define ll long long
8 using namespace std;
9 const int inf=1e9;
10 const int mod=1e9+7;
11 const int maxn=2e5+10;
12 int n;
13 int a[maxn];
14 int l[maxn],r[maxn];
15 int main(){
16 ios::sync_with_stdio(false);
17 //freopen("in","r",stdin);
18 cin>>n;
19 int ans=0;
20 for(int i=1;i<=n;i++){
21 cin>>a[i];
22 if(a[i]>a[i-1]) l[i]=l[i-1]+1;
23 else l[i]=1;
24 ans=max(ans,l[i]);
25 }
26 r[n]=1;
27 for(int i=n-1;i>=1;i--){
28 if(a[i]<a[i+1]) r[i]=r[i+1]+1;
29 else r[i]=1;
30 }
31 for(int i=2;i<n;i++){
32 if(a[i+1]>a[i-1]) ans=max(ans,l[i-1]+r[i+1]);
33 }
34 cout<<ans<<endl;
35 return 0;
36 }
类似于B 的隔板法,将遍历过程中不符合严格单调递增的作为板子,进行分割。
但是分割之后,隔间与隔间是相互联系的,与走楼梯的经典迭代问题相似。
这题关键就在于怎么巧妙运用隔板法和迭代编写程序。
这题虽然很简单,但是改了好几次,没办法,菜是原罪。
CF 1272 D. Remove One Element的更多相关文章
- [CodeForces - 1272D] Remove One Element 【线性dp】
[CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...
- Codeforces Round #605 (Div. 3) D. Remove One Element(DP)
链接: https://codeforces.com/contest/1272/problem/D 题意: You are given an array a consisting of n integ ...
- how to remove an element in lxml
import lxml.etree as et xml=""" <groceries> <fruit state="rotten"& ...
- CF1272D. Remove One Element 题解 动态规划
题目链接:http://codeforces.com/contest/1272/problem/D 题目大意: 给你一个长度为 \(n\) 的数组,你最多删除一个元素(也可以不删),求此条件限制下的最 ...
- 【cf比赛记录】Codeforces Round #605 (Div. 3)
比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...
- CF 768B
CF 768B题意:In each operation Sam must remove any element x, such that x>1, from the list and inser ...
- Codeforces 1272 A-E
Codeforces 1272 A-E A Three Friends 直接枚举所有情况,共\(3\times 3\times 3=27\)种. code #include<bits/stdc+ ...
- jQuery之empty、remove、detach
三者都有把元素移除的作用,但细微的差别,造就了它们的使命不同. 最权威的解释当然是jQuery_API咯,下面是API中关于他三儿的部分截取. 一.empty: This method removes ...
- 402. Remove K Digits
(English version is after the code part) 这个题做起来比看起来容易,然后我也没仔细想,先速度刷完,以后再看有没有改进. 用这个来说: 1 2 4 3 2 2 1 ...
- [Swift]LeetCode703. 数据流中的第K大元素 | Kth Largest Element in a Stream
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
随机推荐
- startActivity 新开一个Activity
private void startActivity(Intent intent) { Context ctx = ApplicationController.getTopActivity(); if ...
- 解题报告:Codeforces 279C Ladder
Codeforces 279C Ladder 本题与tbw这篇博客上的题有相似思路.tbw的本来我还不会,抄了题解才过,这道题好歹自己磕半天磕出来了.到tbw做那道题我突然想明白了,再一想诶跟这里不是 ...
- centos6.5升级python3.6并安装boto3模块
1.先升级openssl yum安装各种依赖,yum install gcc gcc-c++ autoconf automake zlib zlib-devel pcre-devel tar zxvf ...
- 查询dockerhub中某镜像所有版本
curl https://registry.hub.docker.com/v1/repositories/${imagename}/tags | tr -d '[[]" ]' | tr '} ...
- 淘淘商城项目技术点-9:使用FTPClient及FtpUtil工具类将图片上传至ngnix图片服务器
package com.taotao.controller; import com.taotao.common.utils.FtpUtil; import org.apache.commons.net ...
- vue 使用 swiper vue-awesome-swiper
文档地址 https://github.com/surmon-china/vue-awesome-swiper 演示地址 https://v1.github.surmon.me/vue-awesome ...
- mysql 消息表分区方案
首先先看消息表创建脚本 我们用hash分区 在字段 user_id 分成100个区 CREATE TABLE `messages` ( `id` int(10) unsigned NOT NULL A ...
- JDBC之ResultSet和元数据
ResultSet 从名字上就可以看到是结果集,表示的是查询出来的结果集. JDBC用ResultSet来封装结果集,查询结果表的对象. 查询结果分为两种情况: 单值 单个结果,比如说SQL如下: s ...
- Github的.gitignore忽略文件
Git中有一个非常重要的一个文件-----.gitignore 1.当然如果已经push了怎么办?当然也有解决方法,如下: 有时候在项目开发过程中,突然心血来潮想把某些目录或文件加入忽略规则,按照上述 ...
- 单文件WSDL,非模块化
最近在使用CXF做WebService Sever端,接口与实现类不在一个包下. 实现类如下: 1 @WebService(serviceName = "Demo" 2 , tar ...