Codeforces Round #FF (Div. 2):C. DZY Loves Sequences[通俗易懂]

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

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.

题意就是, 你能够改变字符串中的一个字符。 就出其最长的连续字串

如案列, 7 2 3 1 5 6 —————7 2 3  4 5 6

输出即为5.


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<sstream>
#include<cmath>

using namespace std;

#define f1(i, n) for(int i=0; i<n; i++)
#define f2(i, m) for(int i=1; i<=m; i++)
#define f3(i, n) for(int i=n; i>=1; i--)
#define f4(i, n) for(int i=1; i<=n; i++)
#define f5(i, n) for(int i=2; i<=n; i++)
#define M 1005

const int INF = 0x3f3f3f3f;
int n, a[100005], b[100005];

int main()
{
    cin>>n;
    f4(i, n)
    cin>>a[i];
    b[1]=1;
    f5(i, n)
    {
        b[i]=1;
        if (a[i]>a[i-1])
            b[i]=b[i-1]+1;
    }
    int ans=-INF;
    f3(i, n)
    {
        if (b[i]==n)
            ans=max(b[i], ans);
        else
            ans=max(ans, b[i]+1);
        if (a[i-b[i]+1]-1>a[i-b[i]-1])
            ans=max(ans,b[i]+b[i-b[i]]);
        if (a[i-b[i]+2]-1>a[i-b[i]])
            ans=max(ans,b[i]+b[i-b[i]]);
    }
    cout<<ans<<endl;
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116123.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 原码反码补码运算规则_正数的原码反码补码相同吗

    原码反码补码运算规则_正数的原码反码补码相同吗一.机器数和真值在学习原码,反码和补码之前,需要先了解机器数和真值的概念.1、机器数一个数在计算机中的二进制表示形式,叫做这个数的机器数。机器数是带符号的,在计算机用一个数的最高位存放符号,正数为0,负数为1.比如,十进制中的数+3,计算机字长为8位,转换成二进制就是00000011。如果是-3,就是10000011。那么,这里的00000011和10000011就是机器数。2、因为第一位是符号位,所以机器数的形式值就不等于真正的数值。例如上面的有符号数100

    2022年9月23日
    3
  • 软件介绍网站:“软矿”x-berry「建议收藏」

    软件介绍网站:“软矿”x-berry「建议收藏」今天发现了一个推荐软件的网站叫做“软矿”做的很不错。页面简洁,大方,内容也很新。我查询的是虚拟机相关的软件和技术,里面对VMware和Virtualbox的介绍都比较丰富。

    2022年8月3日
    14
  • 解决hash冲突的几种方法_hashmap hash冲突

    解决hash冲突的几种方法_hashmap hash冲突哈希表定义散列表(Hashtable,也叫哈希表),是根据键(Key)而直接访问在内存存储位置的数据结构。也就是说,它通过计算一个关于键值的函数,将所需查询的数据映射到表中一个位置来访问记录,这加快了查找速度。这个映射函数称做散列函数,存放记录的数组称做散列表。实现关键点hash函数hash冲突解决首先来说hash函数,java中对象都已一个hashCode()方法,那为什么还

    2022年8月30日
    4
  • 走近webpack(5)–devtool及babel的使用

    这一章咱们来说一下如何使用babel以及如何用webpack调试代码。这是基础篇的最后一章,这些文章只是罗列的给大家讲解了在一些场景中webpack怎样使用,这章结束后会给大家讲解一下如何在我们实际的

    2022年3月25日
    50
  • docker部署web项目_docker web管理工具

    docker部署web项目_docker web管理工具前言前面我们运行的容器并没有一些什么特别的用处。接下来让我们尝试使用docker构建一个web应用程序。我们将在docker容器中运行一个PythonFlask应用来运行一个web

    2022年7月31日
    5
  • nacicat 15激活码(最新序列号破解)

    nacicat 15激活码(最新序列号破解),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    41

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号