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
5
1 2 5 3 4
output
4 
input
2
1 2
output
2 
input
7
6 5 4 3 2 4 3
output
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.

这个题我自己的写法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的更多相关文章

  1. [CodeForces - 1272D] Remove One Element 【线性dp】

    [CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...

  2. 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 ...

  3. how to remove an element in lxml

    import lxml.etree as et xml=""" <groceries> <fruit state="rotten"& ...

  4. CF1272D. Remove One Element 题解 动态规划

    题目链接:http://codeforces.com/contest/1272/problem/D 题目大意: 给你一个长度为 \(n\) 的数组,你最多删除一个元素(也可以不删),求此条件限制下的最 ...

  5. 【cf比赛记录】Codeforces Round #605 (Div. 3)

    比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...

  6. CF 768B

    CF 768B题意:In each operation Sam must remove any element x, such that x>1, from the list and inser ...

  7. Codeforces 1272 A-E

    Codeforces 1272 A-E A Three Friends 直接枚举所有情况,共\(3\times 3\times 3=27\)种. code #include<bits/stdc+ ...

  8. jQuery之empty、remove、detach

    三者都有把元素移除的作用,但细微的差别,造就了它们的使命不同. 最权威的解释当然是jQuery_API咯,下面是API中关于他三儿的部分截取. 一.empty: This method removes ...

  9. 402. Remove K Digits

    (English version is after the code part) 这个题做起来比看起来容易,然后我也没仔细想,先速度刷完,以后再看有没有改进. 用这个来说: 1 2 4 3 2 2 1 ...

  10. [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 ...

随机推荐

  1. CSS渐变样色的字

    width: 118px; height: 17px; font-size: 13px; font-family: MicrosoftYaHei-Bold, MicrosoftYaHei; font- ...

  2. 【NPDP专项练习】第五章 工具与绩效度量

    第五章 工具与绩效度量 1.这是一种使用一系列的迭代轮数通过专家组对未来可能的一种预测 A 决策一制作方法论 B 德尔菲 C 实施路线 D 组合标准 答案:B 解析 德尔菲流程(DelphiProce ...

  3. accept 类型列表

    *.3gpp audio/3gpp, video/3gpp 3GPP Audio/Video *.ac3 audio/ac3 AC3 Audio *.asf allpication/vnd.ms-as ...

  4. C# List GroupBy and Sum

    List<PartRequest> partRequests = new List<PartRequest>(); partRequests.Add(new PartReque ...

  5. 记一次mysql5.7保存Emoji表情

    1.错误:SQLException; SQL state [HY000]; error code [1366]; Incorrect string value: '\xF0\x9F\x90\x96 \ ...

  6. Finance财务软件(辅助核算专题)

    支持辅助核算和数量核算

  7. CF446D 题解

    题意 传送门 给定一张 \(n\) 个点 \(m\) 条边的无向图,每个节点有权值 \(v_i=\) \(0/1\).角色从节点 \(1\) 开始随机游走,走到 \(n\) 停止.求其经过路径上权值和 ...

  8. plsql和instantclient版本都对,依然不能初始化oci.dll解决办法

    这里写到 "初始化错误,不能初始化 oci.dll, 请确认你安装的是64位的Oracle客户端 " ,这个描述还是非常的到位啊,我一检查,果然下载的客户端是32位的,在确保自己的 ...

  9. VMWare 12 Mac 10.11 XCode 7.3 Ipad真机调试重要问题总结

    XCode 7.3可以不需要每年缴费而直接调试IOS应用,测试如下: 1,安装Mac 10.11在VMWare12上,网上有很多例子.注意: 1.1,虚拟机设置中USB为USB2.0,不能是3.0或其 ...

  10. pgsql指定部分字段去重

    -- 基于ig.start_pile,ig.end_pile 字段去重 with ete as ( SELECT * from (SELECT ROW_NUMBER() OVER(PARTITION ...