首页 > 其他 > 详细

Codeforces Round #605 (Div. 3) D. Remove One Element(DP)

时间:2019-12-16 09:09:04      阅读:120      评论:0      收藏:0      [点我收藏+]

链接:

https://codeforces.com/contest/1272/problem/D

题意:

You are given an array a consisting of n integers.

You can remove at most one element from this array. Thus, the final length of the array is n?1 or n.

Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array.

Recall that the contiguous subarray a with indices from l to r is a[l…r]=al,al+1,…,ar. The subarray a[l…r] is called strictly increasing if al<al+1<?<ar.

思路:

正着反着算一边,能往两边延长的最大长度,对每个点特判。

代码:

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10;

int Dp[MAXN], a[MAXN], Dpr[MAXN];
int n;

int main()
{
    cin >> n;
    for (int i = 1;i <= n;++i)
        cin >> a[i];
    int ans = 1;
    Dp[0] = Dpr[n+1] = 0;
    Dp[1] = Dpr[n] = 1;
    for (int i = 2;i <= n;++i)
    {
        if (a[i] > a[i-1])
            Dp[i] = Dp[i-1]+1;
        else
            Dp[i] = 1;
    }
    for (int i = n-1;i >= 1;--i)
    {
        if (a[i] < a[i+1])
            Dpr[i] = Dpr[i+1]+1;
        else
            Dpr[i] = 1;
    }
    for (int i = 2;i <= n;++i)
    {
        ans = max(ans, Dp[i]);
        if (a[i] > a[i-2])
            ans = max(ans, Dp[i-2]+Dpr[i]);
    }
    cout << ans << endl;

    return 0;
}

Codeforces Round #605 (Div. 3) D. Remove One Element(DP)

原文:https://www.cnblogs.com/YDDDD/p/12046674.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!