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. 利用github pages五分钟建好个人网站+个人博客

    笔者自己在建个人网站/个人博客的时候其实遇到了不少麻烦,但是都一一解决了,这里教给大家最简单的方式. 首先你需要一个GitHub账号,访问https://github.com创建新账号即可. 然后访问 ...

  2. 2->集群架构主机优化流程

    集群架构优化流程: 有道笔记分享链接

  3. SLF4J multiple

    "C:\Program Files\Java\jdk1.8.0_65\bin\java" -Didea.launcher.port=7537 "-Didea.launch ...

  4. shell中的特殊符号总结

    在shell中常用的特殊符号罗列如下: # ;   ;; . , / \\ 'string'| !   $   ${}   $? $$   $* \"string\"* **   ...

  5. 20165333 学习基础和C语言学习基础

    说实话,我并没有什么技能比90%以上的人更好,非要拿一个出来的话,篮球勉强好一点吧.最初接触篮球是小学的时候跟着哥哥看他打球,哥哥的球技在同龄人中算是好的,每次看他各种突破过人,我都觉得特别潇洒帅气, ...

  6. C#socket编程序(二)

    在上一篇中,我列了一些常用的方法,可以说这些方法是一些辅助性的方法,对于分析网络中的主机属性非常有用.在这篇中,我将会介绍一下面向连接(TCP)socket编程,其中辅以实例,代码可供下载. 对于TC ...

  7. codeforces 603 A

    题目大意:给你一个0,1串, 你可以反转一段连续的区间,问你最长的合法子串是多长, 合法字串相邻的两个不能相同. 思路:dp[ i ][ k ][ j ] 表示到第 i 个字符, 处于k这种状态, k ...

  8. 手工释放linux内存------/proc/sys/vm/drop_cache

    当在Linux下频繁存取文件后,物理内存会很快被用光,当程序结束后,内存不会被正常释放,而是一直作为caching.这个问题,貌似有不少人在问,不过都没有看到有什么很好解决的办法.那么我来谈谈这个问题 ...

  9. HDU - 1716 排列2 水题

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  10. spring中的scope详解

    spring容器中的bean默认是单例模式的,改成非单例模式需要在类上加上@Scope("prototype") 1. scope概论 spring中scope是一个非常关键的概念 ...