C - DZY Loves Sequences

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.

Example

Input
  1. 67 2 3 1 5 6
Output
  1. 5

Note

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

题目意思是,给出一个序列,只能改变一个数的值,使最长上升子序列的长度最长.

这题其实挺水.我们设f[i]为第i个数左边的最长上升子序列的长度,g[i]为第i个数右边的最长上升子序列的长度,

然后在满足a[i-1]+1<=a[i+1]-1的情况下,求一下max(g[i]+f[i]+1)就行了.然后居然WA了......

后来发现,还有种情况没发现,就是以i结尾或开头的子序列,也有可能是最优的→_→

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. ],f1[],f2[],n,ans;
  6. int read(){
  7. ,f=; char ch=getchar();
  8. '){if (ch=='-') f=-f; ch=getchar();}
  9. +ch-',ch=getchar();
  10. return x*f;
  11. }
  12. int main(){
  13. n=read(); ; i<=n; i++) a[i]=read();
  14. ){puts(;}
  15. ){puts(;}
  16. f1[]=,f1[]=;
  17. ; i<=n; i++) ]>a[i-]) f1[i]=f1[i-]+; ;
  18. f2[n]=,f2[n-]=;
  19. ; i>=; i--) ]>a[i+]) f2[i]=f2[i+]+; ;
  20. ans=max(ans,f2[]+); ans=max(ans,f1[n]+);
  21. ; i<n; i++){
  22. ]+<=a[i+]-) ans=max(ans,f1[i]+f2[i]+);
  23. ans=max(ans,f1[i]+);
  24. ans=max(ans,f2[i]+);
  25. }
  26. printf("%d",ans);
  27. ;
  28. }

[CodeForces - 447C] C - DZY Loves Sequences的更多相关文章

  1. Codeforces 447 C DZY Loves Sequences【DP】

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

  2. 【Codeforces 446A】DZY Loves Sequences

    [链接] 我是链接,点我呀:) [题意] 让你找一段连续的区间 使得这一段区间最多修改一个数字就能变成严格上升的区间. 问你这个区间的最长长度 [题解] dp[0][i]表示以i为结尾的最长严格上升长 ...

  3. Codeforces 447C - DZY Loves Sequences

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

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

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

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

  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 (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 Round #FF 446A DZY Loves Sequences

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

  9. cf446A DZY Loves Sequences

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

随机推荐

  1. windows特殊文件或文件夹

    考了很多文章,搜集了很多资料整理而成.好的用途可以用来隐藏个人资料,防止误删,病毒免疫等等.至于坏的方面,当然也可用来隐藏木马等等,就看你怎么用了.还有一个没有搞明白,资料上也没找到,请知道的指点一下 ...

  2. FZU oj 2277 Change 树状数组+dfs序

    Problem 2277 Change Time Limit: 2000 mSec    Memory Limit : 262144 KB  Problem Description There is ...

  3. hdu 3829 Cat VS Dog 二分图匹配 最大点独立集

    Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Prob ...

  4. Redis 个人理解总结

    一.什么是Redis ? Redis(remote dictionnary server)是一个key-value存储系统.Redis是一个开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可 ...

  5. PHP Warning: Module 'memcache' already loaded in Unknown on line 0

    出现类似PHP Warning: Module * already loaded in Unknown on line 0,一般是可能因为升级php导致的组件重复加载,解决就是 1.vi /etc/p ...

  6. ado_基本连接操作【四】

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data. ...

  7. C#发起HTTP请求

    浏览器能看到的数据  用后端模拟请求都能获取到  如果拿不到 看看是不是请求参数哪里没设置 刚好服务器检查了这个参数 string url = ""; string para = ...

  8. anusplina 4.36版本使用提示 说明

    1),必须要注意的是,你生成的dat文件中,第一列必须是气象站点编号:第二列必须是经度数值,而且是投影坐标下,以 米 为单位:第三列必须是纬度了:第四列必须是海拔了:之后就是你自己的数据,比如平均温度 ...

  9. vuex 源码:深入 vuex 之辅助函数 mapState

    前言 当一个组件要获取多个 state 的时候,声明计算属性就会变得重复和冗余了.我们可以使用到辅助函数 mapState 来更快更简洁地生成计算属性. 所以我们得清楚,mapState 的作用就是帮 ...

  10. MySQL学习(六)

    1 注意 select cout(*) from 表名: 查询的就是绝对的行数,哪怕某一列所有字段全部为NULL,也计算在内.而select cout(列名) form 表名:查询的是该列不为null ...