codeforces-446A-DZY Loves Sequences

来源:互联网 发布:橡胶木与橡木区别 知乎 编辑:程序博客网 时间:2024/06/05 08:27

codeforces-446A-DZY Loves Sequences


                    time limit per test1 second     memory limit per test256 megabytes

DZY has a sequence a, consisting of n integers.

We’ll call a sequence ai, ai + 1, …, aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.

Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.

You only need to output the length of the subsegment you find.

Input
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, …, an (1 ≤ ai ≤ 109).

Output
In a single line print the answer to the problem — the maximum length of the required subsegment.

input
6
7 2 3 1 5 6
output
5

Note
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4.

题目链接:cf-446A

题目大意:给出n长度的序列,问可以任意改变一个值,求最长的严格递增子序列长度

题目思路:用ret数组记录前面比当前值小的连续有几个。
例如:a[]: 7 2 3 1 5 6
ret[]: 1 1 2 1 2 3

以下是代码:

#include <vector>#include <map>#include <set>#include <algorithm>#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>using namespace std;int a[100010];int ret[100010];int main(){    int n;    cin >> n;    for (int i = 0; i < n; i++)    {        cin >> a[i];        if (a[i - 1] < a[i])  ret[i] = ret[i - 1] + 1;        else  ret[i] = 1;    }    int maxnum = 0;    int cur = 0;    for (int i = n - 1; i >= 0; i--)    {        maxnum = max(maxnum,ret[i - 1] + 1);        maxnum = max(maxnum,cur + 1);        if (i == 0 || i == n - 1 || a[i - 1] + 1 < a[i + 1])        {            maxnum = max(maxnum,ret[i - 1] + cur + 1);        }        if (a[i] < a[i + 1])  cur++;        else cur = 1;    }     cout << maxnum << endl;    return 0;}
0 0
原创粉丝点击