求出以i下标起始和结束的递增数列长度,然后,若i-1与i的值差值大于2,则可以拼接长度。
#include "cctype"
#include "cstdio"
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
inline void readIn(unsigned int& x) {
static char ch;
while (!isdigit(ch = getchar()));
for (x = -48 + ch; isdigit(ch = getchar()); (x *= 10) += ch - 48);
}
const unsigned int MaxN = 100005;
unsigned int f[MaxN], a[MaxN], g[MaxN], ans, n;
int main() {
register int i;
readIn(n);
for (i = 0; i ^ n; readIn(a[++i]));
for (i = 1; i <= n; ++i)
f[i] = (a[i] > a[i - 1]) ? f[i - 1] + 1 : 1;
for (i = n; i >= 1; --i)
g[i] = (a[i] < a[i + 1]) ? g[i + 1] + 1 : 1;
for (i = 1; i <= n; ++i)//没有拼接之前的最大值
ans = max(g[i] + 1, max(ans, f[i] + 1));
for (i = 1; i <= n; ++i)//拼接之后的最大值
(a[i + 1] >= a[i - 1] + 2) ?
ans = max(ans, f[i - 1] + g[i + 1] + 1) : 1;
printf("%d\n", ans);
}