预处理出每一个数字能够向后延伸多少,然后尝试将两段拼起来。

C. DZY Loves Sequences
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 test(s)
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.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int n,a[110000],dp[110000]; int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",a+i);
int nx=-1;
for(int i=0;i<n;i++)
{
if(nx>i)
{
dp[i]=nx-i;
continue;
}
int j=i;
while(j+1<n&&a[j]<a[j+1]) j++;
dp[i]=j-i+1;
nx=max(nx,j+1);
}
int ans=dp[0];
for(int i=0;i<n;i++)
{
int p=dp[i]+i-1;
if(p-1>=0&&p+1<n&&a[p+1]>a[p-1]+1)
{
ans=max(ans,dp[i]-1+dp[p+1]+1);
}
if(p+2<n&&a[p+2]>a[p]+1)
{
ans=max(ans,dp[i]+dp[p+2]+1);
}
if(p+1<n||i-1>=0) ans=max(ans,dp[i]+1);
i=p;
}
printf("%d\n",ans);
return 0;
}

Codeforces Round #FF 446A DZY Loves Sequences的更多相关文章

  1. codeforces 446A DZY Loves Sequences

    vjudge 上题目链接:codeforces 446A 大意是说最多可以修改数列中的一个数,求最长严格递增的连续子序列长度. 其实就是个 dp 的思想,想好思路后交上去没想到一直 wa 在第二个测试 ...

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

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

  3. CodeForces 446A DZY Loves Sequences (DP+暴力)

    题意:给定一个序列,让你找出一个最长的序列,使得最多改其中的一个数,使其变成严格上升序列. 析:f[i] 表示以 i 结尾的最长上升长度,g[i] 表示以 i 为开始的最长上升长度,这两个很容易就求得 ...

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

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

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

  6. Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划

    A. DZY Loves Sequences 题目连接: http://www.codeforces.com/contest/446/problem/A Description DZY has a s ...

  7. 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 ...

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

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

  9. [CodeForces - 447C] C - DZY Loves Sequences

    C - DZY Loves Sequences DZY has a sequence a, consisting of n integers. We'll call a sequence ai, ai ...

随机推荐

  1. 在SQL Server Management Studio中可以运行作业但是用T-SQL运行则失败

    原文:在SQL Server Management Studio中可以运行作业但是用T-SQL运行则失败 问题: 在SQL Server Management Studio中可以运行作业但是用T-SQ ...

  2. 减少iPhone手机系统版本号

    要减少移动版本号,本号,或者到"威风"站点或其他能够下载到iPhone固件的站点上下载固件,然后打开iTunes,进入到 然后按着(alt),同一时候用鼠标点击(更新button) ...

  3. 采用Visual Stuidio 2010 创建网站栏

    采用Visual Stuidio 2010 创建网站栏 Visual Stuidio 2010 该项目模板使创建网站栏/内容类型和列表变得非常方便. 1. 管理员身份打开Visual Stuidio ...

  4. (大数据工程师学习路径)第一步 Linux 基础入门----环境变量与文件查找

    环境变量与文件查找 本节介绍环境变量的作用与用法,及几种搜索文件的方法.学会这些技巧高效地使用 Linux. 一.环境变量 1.变量 要解释环境变量,得先明白变量是什么,准确的说应该是 Shell 变 ...

  5. java自动转型

    /*2015-10-30*/ public class TypeAutoConvert { public static void main(String[] args) { int a = 5; Sy ...

  6. 基于注释配置bean和装饰bean

    1.组件扫描 Spring容器能够从classpath(类路径)下自动扫描.侦测和实例化具有特定注释的组件. 2.特定注释组件 –@Component: 基本注解, 标识了一个受 Spring 管理的 ...

  7. obj-c编程04:类的继承

    这第4篇内容比較少,主要说的是obj-c中的类的继承,须要说明的是我仅仅是写了继承中最简单的形式,假设所有展开来说,那就多了去了!关键是如今肚子里还没装够墨水,没法展开啊! 以下的代码中,我们写了2个 ...

  8. kendo ui 单击取消编辑数据grid减少的原因和治疗方法的数据

    kendo ui单击取消编辑数据buttongrid数据缩减.原因grid编辑数据追打datasorce于data寻找阵列数据的存在.假定有不会加入,加入缺席. 首先一个样本: html代码: < ...

  9. 漫游Kafka实战篇clientAPI

    原文地址:http://blog.csdn.net/honglei915/article/details/37697655 Kafka Producer APIs 旧版的Procuder API有两种 ...

  10. AJAX 怎样在一个UpDatePanel中刷新另一个updatePanel

    原文:AJAX 怎样在一个UpDatePanel中刷新另一个updatePanel 在页面上(.aspx)<asp:UpdatePanel ID="MyID1" runat= ...