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.

Examples
Input

Copy
6
7 2 3 1 5 6
Output

Copy
5
Note

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

问最多修改一个数字,序列可获得地最大严格递增字段长度为多大;

考虑dp;

dp1 表示以 i 位置结尾的最长子段长度;

dp2 表示以 i 位置开头的最长子段长度;

特判一下当 n=1时,长度为1;

考虑拼接:当 x[ i+1 ]>=2+ x[ i-1 ]时,那么改变 x[ i ]即可拼接子段

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int n;
int x[maxn]; int main() {
//ios::sync_with_stdio(0);
cin >> n;
vector<int>dp1(maxn, 1);
vector<int>dp2(maxn, 1);
for (int i = 0; i <= n; i++)dp1[i] = dp2[i] = 1;
for (int i = 0; i < n; i++)rdint(x[i]);
if (n < 2) {
cout << 1 << endl; return 0;
}
for (int i = 1; i < n; i++)
dp1[i] = (x[i] > x[i - 1]) ? dp1[i - 1] + 1 : 1;
for (int i = n - 2; i >= 0; i--)
dp2[i] = (x[i + 1] > x[i]) ? dp2[i + 1] + 1 : 1;
int ans = 0;
for (int i = 1; i < n; i++)ans = max(ans, dp1[i - 1] + 1);
for (int i = 0; i < n; i++)ans = max(dp2[i + 1] + 1, ans);
for (int i = 1; i <= n - 1; i++) { if (x[i + 1] - x[i - 1] >= 2) {
ans = max(ans, dp1[i - 1] + 1 + dp2[i + 1]);
}
}
cout << ans << endl;
return 0;
}

CF446A DZY Loves Sequences 简单dp的更多相关文章

  1. cf446A DZY Loves Sequences

    A. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. codeforces#FF DIV2C题DZY Loves Sequences(DP)

    题目地址:http://codeforces.com/contest/447/problem/C C. DZY Loves Sequences time limit per test 1 second ...

  3. Codeforces 447 C DZY Loves Sequences【DP】

    题意:给出一列数,在这个序列里面找到一个连续的严格上升的子串,现在可以任意修改序列里面的一个数,问得到的子串最长是多少 看的题解,自己没有想出来 假设修改的是a[i],那么有三种情况, 1.a[i]& ...

  4. Codeforces 446A. DZY Loves Sequences (线性DP)

    <题目链接> 题目大意: 给定一个长度为$n$的序列,现在最多能够改变其中的一个数字,使其变成任意值.问你这个序列的最长严格上升子段的长度是多少. #include <bits/st ...

  5. CodeForces - 446A DZY Loves Sequences(dp)

    题意:给定一个序列a,求最长的连续子序列b的长度,在至多修改b内一个数字(可修改为任何数字)的条件下,使得b严格递增. 分析: 1.因为至多修改一个数字,假设修改a[i], 2.若能使a[i] < ...

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

    题目传送门 /* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + ...

  7. Codeforces Round #FF 446A DZY Loves Sequences

    预处理出每一个数字能够向后延伸多少,然后尝试将两段拼起来. C. DZY Loves Sequences time limit per test 1 second memory limit per t ...

  8. Codeforces 447C - DZY Loves Sequences

    447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...

  9. Codeforces Round #FF (Div. 2):C. DZY Loves Sequences

    C. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

随机推荐

  1. 通知消息与ON_NOTIFY

    1.通知消息一般是由子控件发出,由父窗口响应,因此响应函数的位置在父窗口内. 2.通知消息发送给父窗口的是通知码,即WM_NOTIFY消息(但为了区分方便不同的消息有不同的名称,但都是以WM_NOTI ...

  2. 《java编程思想》:散列的原理

    以实现一个简单的HashMap为例,详细讲解在code之中. 简单解释散列原理: 1.map中内建固定大小数组,但是数组并不保存key值本身,而是保存标识key的信息 2.通过key生成数组角标,对应 ...

  3. Java 集合常用特点

    1:集合:1) Collection(单列集合) ---------------------------------------------------------------------       ...

  4. freeMarker(十四)——XML处理指南之必要的XML处理

    学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 1.基本内容 假设程序员在数据模型中放置了一个XML文档,就是名为 d ...

  5. BZOJ1799 [Ahoi2009]self 同类分布[数位DP]

    求出[a,b]中各位数字之和能整除原数的数的个数. 有困难的一道题.被迫看了题解:枚举每一个各位数字的和($<=162$),设计状态$f[len][sum][rest]$表示dp后面$len$位 ...

  6. ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)

    Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...

  7. html之ajax

    正常情况下,html中的ajax(也就是XMLHttpRequest对象)是不能跨域的.(特殊情况,此处不讨论,请网上Google) ---跨域:是url的协议或ip或端口,其中有一个不同,就是跨域. ...

  8. 使用Rancher搭建K8S测试环境

    使用Rancher搭建K8S测试环境 http://blog.csdn.net/csdn_duomaomao/article/details/75316926 环境准备(4台主机,Ubuntu16.0 ...

  9. Log4Net日志记录介绍

    原文地址 : http://www.cnblogs.com/wolf-sun/p/3347373.html#3009010 简介 log4net库是Apache log4j框架在Microsoft . ...

  10. 百度之星 hdu5701 中位数计数

    http://acm.hdu.edu.cn/showproblem.php?pid=5701 给出一个序列,取其中的任何一个连续的序列,该序列的数从小到大排列,待更新,,, #include<i ...