A. DZY Loves Sequences

题目连接:

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

Description

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.

Sample Input

6

7 2 3 1 5 6

Sample Output

5

Hint

题意

让你找到一个区间,你可以改变这个区间的一个数,然后使得这个区间是严格上升的

且这个区间一定是最长的,输出区间长度

题解:

dp1[i]表示从左边开始的最长上升子串,dp2[i]是右边开始的最长上升子串

然后我们枚举i,那么答案就显然是在a[i-1]<a[i+1]-1的情况下,ans=max(ans,dp1[i-1]+dp2[i+1]+1)这个玩意儿

然后不停转移就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int dp1[maxn],dp2[maxn],a[maxn];
int main()
{
int n;
scanf("%d",&n);a[0]=1e9+7,a[n+1]=1e9+7;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
if(n==1)return puts("1"),0;
if(n==2)return puts("2"),0;
int ans=0;
for(int i=1;i<=n;i++)
{
dp1[i]=1;
if(a[i]>a[i-1])dp1[i]=dp1[i-1]+1;
ans=max(ans,dp1[i]);
}
for(int i=n;i>=1;i--)
{
dp2[i]=1;
if(a[i]<a[i+1])dp2[i]=dp2[i+1]+1;
ans=max(ans,dp2[i]);
}
for(int i=1;i<=n;i++)
{
if(a[i-1]<a[i+1]-1)
ans=max(ans,dp1[i-1]+dp2[i+1]+1);
}
for(int i=2;i<=n;i++)ans=max(ans,dp2[i]+1);
for(int i=1;i<n;i++)ans=max(ans,dp1[i]+1);
cout<<ans<<endl;
}

Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划的更多相关文章

  1. 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] + ...

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

    题目链接: http://www.codeforces.com/contest/446/problem/A 题解: dp1[x]表示以x结尾的最大严格升序连续串,dp2[x]表示以x开头的最大严格升序 ...

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

    解题报告:输入一个数列,选取一个子数列,要求最多只能改动这个子数列中的一个数,使得这个子数列是严格的升序的(严格升序没有相等的) 我的做法是,第一步把这个 数列的每个升序的子数列都找出来,然后看这些子 ...

  4. Codeforces Round #FF (Div. 1) B. DZY Loves Modification 优先队列

    B. DZY Loves Modification 题目连接: http://www.codeforces.com/contest/446/problem/B Description As we kn ...

  5. Codeforces Round #FF (Div. 2) D. DZY Loves Modification 优先队列

    D. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. Codeforces Round #FF (Div. 1) B. DZY Loves Modification

    枚举行取了多少次,如行取了i次,列就取了k-i次,假设行列单独贪心考虑然后相加,那么有i*(k-i)个交点是多出来的:dpr[i]+dpc[k-i]-i*(k-i)*p 枚举i取最大值.... B. ...

  7. Codeforces Round #FF (Div. 2):B. DZY Loves Strings

    B. DZY Loves Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #FF (Div. 2)__E. DZY Loves Fibonacci Numbers (CF447) 线段树

    http://codeforces.com/contest/447/problem/E 题意: 给定一个数组, m次操作, 1 l r 表示区间修改, 每次 a[i] +  Fibonacci[i-l ...

  9. Codeforces Round #FF (Div. 2) A. DZY Loves Hash

    DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the ...

随机推荐

  1. Linux查看用户密码修改时间

    在/etc/shadow文件里面,第三个字段标识表示密码修改日期:这个是表明上一次修改密码的日期与1970-1-1相距的天数.如果账户自创建后,没有修改过密码,就可以使用这个字段来查找账号创建日期. ...

  2. input 标签禁止输入

    1.鼠标可以点击输入框,但是不能输入 readonly 例如: <input class="layui-input" readonly > 2.鼠标点击输入框出现禁用图 ...

  3. Pytorch数据变换(Transform)

    实例化数据库的时候,有一个可选的参数可以对数据进行转换,满足大多神经网络的要求输入固定尺寸的图片,因此要对原图进行Rescale或者Crop操作,然后返回的数据需要转换成Tensor如: import ...

  4. idea开发工具下载安装教程

    我用这款工具主要用于java开发 在安装这个工具之前需要配置java的环境 java的jdk环境配置 jdk:1.8 jdk官网下载链接 --->点我 进入之后,下拉  选择 jdk1.8版本 ...

  5. tmux 使用

    tmux命令参数 tmux new -s name //创建一个新会话 tmux ls //列出所有会话 tmux a -t name //返回某一个会话 tmux内部命令(ctrl+b之后按) s ...

  6. python连接hbase

    安装HBase HBase是一个构建在HDFS上的分布式列存储系统,主要用于海量结构化数据存储.这里,我们的目标只是为Python访问HBase提供一个基本的环境,故直接下载二进制包,采用单机安装.下 ...

  7. SQL group 分组查询

    1.使用group by进行分组查询  在使用group by关键字时,在select列表中可以指定的项目是有限制的,select语句中仅许以下几项:  被分组的列 为每个分组返回一个值得表达式,例如 ...

  8. javascript json字符串转json对象方法

    /* * @method 将拼接好字符串格式的json 转成json对象 * @param jsonData param fomart: * var jsonData = "{name1:' ...

  9. Adapter 适配器

    意图 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 动机 在软件开发中,有的时候系统的数据和行为都正确,但接口不符合,我们应 ...

  10. python二叉树简单实现

    二叉树简单实现: class Node: def __init__(self,item): self.item = item self.child1 = None self.child2 = None ...