题目地址:http://codeforces.com/contest/447/problem/C

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.


能够先进行预处理。将每个数字可向左延伸的数目记录下来,再将可向右延伸的记录下来,最后遍历一遍,假设这个数能够变成大于左边小于右边的数。即右边-左边>1。这是就能够为左边延伸的+右边延伸的+1。假设不行,则仅仅能满足某一边,这是就要为左边或右边较大的延伸数+1.

代码例如以下。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>
using namespace std;
int a[200000], b[200000], c[200000];
int main()
{
int n, i, j, max1=-1;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
b[0]=1;
for(i=1;i<n;i++)
{
if(a[i]>a[i-1])
{
b[i]=b[i-1]+1;
}
else
b[i]=1;
}
c[n-1]=1;
for(i=n-2;i>=0;i--)
{
if(a[i]<a[i+1])
c[i]=c[i+1]+1;
else
c[i]=1;
}
if(n==1)
printf("1\n");
else
{max1=2;
if(max1<b[n-2]+1)
max1=b[n-2]+1;
if(max1<c[1]+1)
max1=c[1]+1;
for(i=1;i<=n-2;i++)
{
if(a[i+1]-a[i-1]>1)
{
if(max1<b[i-1]+c[i+1]+1)
max1=b[i-1]+c[i+1]+1;
}
else
{
if(max1<b[i-1]+1)
max1=b[i-1]+1;
if(max1<c[i+1]+1)
max1=c[i+1]+1;
}
}
printf("%d\n",max1);}
return 0;
}

codeforces#FF DIV2C题DZY Loves Sequences(DP)的更多相关文章

  1. CodeForces 447C DZY Loves Sequences DP

    题目:click here 题意:求给定序列更改其中一个元素后的最长连续上升子序列的长度 分析:最长的连续子序列有2种,一种是严格上升(没有更改元素)的长度加1,一种是两段严格上升的加起来. #inc ...

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

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

  3. codeforces#FF(div2) D DZY Loves Modification

    首先要知道选择行列操作时顺序是无关的 用两个数组row[i],col[j]分别表示仅选择i行能得到的最大值和仅选择j列能得到的最大值 这个用优先队列维护,没选择一行(列)后将这行(列)的和减去对应的n ...

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

  5. Codeforces Round #FF 446A DZY Loves Sequences

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

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

  9. Codeforces 447C - DZY Loves Sequences

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

随机推荐

  1. svn版本控制

    svn自动生成X.java.mine,X.java.r2333,X.java.r2368文件 这是你和别人的操作形成了冲突: 你和别人都从2333这个版本对X.java进行了修改, 别人先修改之后,先 ...

  2. 一行统计shell

    cat count.log | awk '{print $7}' | awk '{if ($1 == "-") empty++ }END {print NR, empty, emp ...

  3. JQurey获取radio和checkbox的值

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Web2.aspx.cs&q ...

  4. IO之读入文件

    整个java.io包中最重要的就是5个类和一个接口,5个类指的是File,OutputStream,InputStream,Reader,Writer:一个接口是Serializable. 在整个io ...

  5. [LeetCode]题解(python):137-Single Number II

    题目来源: https://leetcode.com/problems/single-number-ii/ 题意分析: 给定一个数组,数组里面每一个数字都出现了3次除了一个,找出那个数.要求时间复杂度 ...

  6. Tensorflow tflearn 编写RCNN

    两周多的努力总算写出了RCNN的代码,这段代码非常有意思,并且还顺带复习了几个Tensorflow应用方面的知识点,故特此总结下,带大家分享下经验.理论方面,RCNN的理论教程颇多,这里我不在做详尽说 ...

  7. Word2Vec在Tensorflow上的版本以及与Gensim之间的运行对比

    接昨天的博客,这篇随笔将会对本人运行Word2Vec算法时在Gensim以及Tensorflow的不同版本下的运行结果对比.在运行中,参数的调节以及迭代的决定本人并没有很好的经验,所以希望在展出运行的 ...

  8. linux环境变量配置总结

    LD_LIBRARY_PATH: 动态库的查找路径设置:方法一: export  LD_LIBRARY_PATH=LD_LIBRARY_PATH:/XXX 但是登出后就失效方法二: 修改~/.bash ...

  9. 2013杭州网络赛D题HDU 4741(计算几何 解三元一次方程组)

    Save Labman No.004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  10. hdu 4612 Warm up 双连通缩点+树的直径

    首先双连通缩点建立新图(顺带求原图的总的桥数,事实上因为原图是一个强连通图,所以桥就等于缩点后的边) 此时得到的图类似树结构,对于新图求一次直径,也就是最长链. 我们新建的边就一定是连接这条最长链的首 ...