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

题目链接:

http://www.codeforces.com/contest/446/problem/A

题解:

dp1[x]表示以x结尾的最大严格升序连续串,dp2[x]表示以x开头的最大严格升序连续串

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<list>
#include<stack>
#include<algorithm>
using namespace std;

const int maxn = 100000 + 10;
const int INF = 2e9;
typedef __int64 LL;

int n;
int dp1[maxn], dp2[maxn];
int arr[maxn];

void init() {
    memset(dp1, 0, sizeof(dp1));
    memset(dp2, 0, sizeof(dp2));
}

int main() {
    while (scanf("%d", &n) == 1 && n) {
        init();
        for (int i = 0; i < n; i++) scanf("%d", arr + i);
        dp1[0] = 1;
        for (int i = 1; i < n; i++) {
            if (arr[i] > arr[i - 1]) dp1[i] = dp1[i - 1] + 1;
            else dp1[i] = 1;
        }
        dp2[n - 1] = 1;
        for (int i = n - 2; i >= 0; i--) {
            if (arr[i] < arr[i + 1]) dp2[i] = dp2[i + 1] + 1;
            else dp2[i] = 1;
        }
        int ans = max(dp2[1] + 1, dp1[n - 2]+1);
        for (int i = 1; i < n - 1; i++) {
            if (arr[i - 1] < arr[i + 1] - 1) ans = max(ans, dp1[i - 1] + 1 + dp2[i + 1]);
            else {
                ans = max(ans,dp1[i - 1] + 1);
                ans = max(ans,dp2[i + 1] + 1);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/fenice/p/5541367.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值