码迷,mamicode.com
首页 > 其他好文 > 详细

DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences

时间:2015-07-18 19:48:44      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:

 

题目传送门

/*
     DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生:
        1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + 1
        2. l[i-1] + 1   3. r[i+1] + 1   //修改a[i]
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int a[MAXN];
int l[MAXN], r[MAXN];
int n;

int main(void)      //Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
{
    scanf ("%d", &n);    int ans = 0;
    for (int i=1; i<=n; ++i)
    {
        scanf ("%d", &a[i]);
        l[i] = 1;
        if (i > 1 && a[i] > a[i-1])    l[i] = l[i-1] + 1;
    }

    for (int i=n; i>=1; --i)
    {
        r[i] = 1;
        if (i < n && a[i] < a[i+1])    r[i] = r[i+1] + 1;
    }

    for (int i=1; i<=n; ++i)
    {
        ans = max (ans, l[i]);    ans = max (ans, r[i]);
        if (i > 1 && i < n && a[i-1] + 1 < a[i+1])    ans = max (ans, l[i-1] + r[i+1] + 1);
        if (i > 1)    ans = max (ans, l[i-1] + 1);
        if (i < n)    ans = max (ans, r[i+1] + 1);
    }

    printf ("%d\n", ans);

    return 0;
}

 

DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences

标签:

原文地址:http://www.cnblogs.com/Running-Time/p/4657341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!