题目链接:http://codeforces.com/contest/447/problem/C
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.
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
In a single line print the answer to the problem — the maximum length of the required subsegment.
6 7 2 3 1 5 6
5
You can choose subsegment a2, a3, a4, a5, a6 and change its 3rd element (that is a4) to 4.
预处理出以i开始的最长递增子串,以i结尾的最长递增子串,然后就扫一遍得结果!
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
const int maxn = 100000 + 10;
int num[maxn];
int l[maxn];
int r[maxn];
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d", &num[i]);
if (n == 1)
printf("%d\n", 1);
else {
l[0] = 1;
for (int i = 1; i < n; ++i) {
if (num[i] > num[i - 1])
l[i] = l[i - 1] + 1;
else
l[i] = 1;
}
r[n - 1] = 1;
for (int i = n - 2; i >= 0; --i) {
if (num[i] < num[i + 1])
r[i] = r[i + 1] + 1;
else
r[i] = 1;
}
int re;
if (r[0] < n)
re = r[0] + 1;
else
re = r[0];
int tmp;
for (int i = 1; i < n - 1; ++i) {
if (num[i - 1] < num[i] && num[i] < num[i + 1])
tmp = l[i - 1] + r[i + 1] + 1;
else if (num[i + 1] > num[i - 1] + 1)
tmp = l[i - 1] + r[i + 1] + 1;
else
tmp = max(l[i - 1], r[i + 1]) + 1;
if (tmp > re)
re = tmp;
}
if (l[n - 1] == n)
re = n;
else if (l[n - 1] + 1 > re)
re = l[n - 1] + 1;
printf("%d\n", re);
}
}