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

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. JAVA运行程序代码段

    我记得那时候刚毕业.学习JAVA恐惧是这里,它是关于JAVA称号.我总是不正确.如今,这最后审查.看了好半天.得赶紧把刚才学习到的那点东西记下来. 一.关于static的代码段运行顺序 运行结果例如以 ...

  2. “NET网络”进行中,多管齐下的人才力挫“”粗俗

            随着互联网的迅猛发展,一些不太干净.低俗的甚至色情的内容不断浮现.不仅严重影响了我们的上网体验,也成为扰乱互联网正常秩序的罪魁祸首,部分不法内容甚至给网民造成了一定的財产损失.在这样的 ...

  3. Androids含文档erver结束(工具包 Httputils)两

    在同server在...的基础上,本文client还登录界面 Andriod简单http get请求基础上,用户注冊后跳转到下载界面,本文下载界面仅仅有两个View,一个是textView显示注冊后u ...

  4. 先锋军Android注射技术《三》

    继续 于<两>通过专门出台ptrace实施注射的技术解决方案,在这一章,我就为大家介绍一Android在独特的喷射技术,我点了他的名字--Component Injection.顾名思义. ...

  5. 微软Visual Studio &quot;14&quot; CTP 2 公布

     对于在微软阵营下进行工作的团队来说,拥有最新版本号的Visual Studio是提高效率最佳的选择,没有之中的一个. 在本文中,我们就上个月公布的Visual Studio "14&q ...

  6. hdu 3911 Black And White(线段树)

    题目连接:hdu 3911 Black And White 题目大意:给定一个序列,然后有M次操作: 0 l r:表示询问l,r中最大连续1的个数 1 l r:表示将l,r区间上的数取反 解题思路:线 ...

  7. HDU 3853 LOOPS 可能性dp(水

    在拐~ #include <stdio.h> #include <cstring> #include <iostream> #include <map> ...

  8. 章节2:SQL之多表连接

    原文:章节2:SQL之多表连接 Sql的多表连接关系有:内连接.外连接和交叉连接. 先建立两个用于演示的表: TB_Characters: Id Character 1 内向 2 外向 3 中性性格 ...

  9. Controller 里面就只应该存放这些不能复用的代码(转)

    MVC 的历史 MVC,全称是 Model View Controller,是模型 (model)-视图 (view)-控制器 (controller) 的缩写.它表示的是一种常见的客户端软件开发框架 ...

  10. 安装程序添加iis的方法经验分享

    原文:安装程序添加iis的方法经验分享 网上有一些这样的方法,但我这里主要做一些对比和扩充 网上这方面的文章的岁数比较大,server 08R2和win7出来后,整理这方面的资料的文章没找到,所以这里 ...