首页 > 其他分享 >codeforces#FF DIV2C题DZY Loves Sequences(DP)

codeforces#FF DIV2C题DZY Loves Sequences(DP)

时间:2023-04-13 21:36:52浏览次数:49  
标签:200000 include codeforces DIV2C FF test output subsegment change


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


C. DZY Loves Sequences



time limit per test



memory limit per test



input



output



a, consisting of n

ai, ai + 1, ..., aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1)

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



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



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;
}



标签:200000,include,codeforces,DIV2C,FF,test,output,subsegment,change
From: https://blog.51cto.com/u_16070138/6188413

相关文章

  • Codeforces Round #257 (Div. 1)B题Jzzhu and Cities(spfa+slf优化)
    题目地址:http://codeforces.com/contest/450/problem/D这题有重边,需要去重。。sad。当时居然没看见。。这题只要引入一个最短路条数,然后再遍历火车线,如果最短路与火车线长度相等,此时如果最短路条数是1的话,那说明这个最短路就是火车线,不能去掉,如果最短路条数大于1条,说明除了这条火车......
  • Educational Codeforces Round 146 (Rated for Div. 2)
    Preface补题ing值得一提的时补这场的时候先是遇上了CF的12小时大维护,后面又遇到了评测机崩了测不了也是有点有意思的说A.Coins傻逼题,首先考虑\(2|n\)时一定有解\(x=\frac{n}{2},y=0\),否则若\(2\nmidn\and2|k\)则由裴蜀定理知此时一定无解否则\(y\)必为奇数,我们令\(x=\fra......
  • 取消加载项提升Office软件打开速度的方法
      本文介绍基于修改加载项,解决MicrosoftOffice系列软件开启速度较慢的办法。  最近,发现Excel软件的打开速度越来越慢,会在一定程度上影响工作效率。因此尝试对此加以解决。其中,本文所给方法对于Word/Excel/PPT文件均适用。但请注意,本文所给出的解决方法仅对由于加载项过多造成......
  • Mac | iOS | Windows:安装Stable diffusion教程
    热烈欢迎,请直接点击!!!进入博主AppStore主页,下载使用各个作品!!!注:博主将坚持每月上线一个新app!!!Apple已支持的开源库:https://machinelearning.apple.com/research/stable-diffusion-coreml-apple-silicon一、MAC部署安装:https://github.com/apple/ml-stable-diffusiongitclone......
  • Understanding the different flavors of Clang C and C++ compilers in Windows
    https://blog.conan.io/2022/10/13/Different-flavors-Clang-compiler-Windows.htmlThisarticlewillexplainthedifferentflavorsofClangCandC++compileryoumightencounterinWindows,andgiveyousomesuggestionsaboutwhichonesmightberightforyo......
  • JAVA使用OpenOffice文件转换
    下载jar包maven中央仓库包不支持docx文件所以不建议使用。jar包是为了方便链接下载链接:https://nchc.dl.sourceforge.net/project/jodconverter/JODConverter/2.2.2/jodconverter-2.2.2.zip 解压后找到:jodconverter-2.2.2\jodconverter-2.2.2\lib\jodconverter-2.2.2.jar放......
  • Google SRE 定义了四个需要监控 延迟(Latency),流量(Traffic),错误(Errors)和饱和度(Saturati
    GoogleSRE定义了四个需要监控的关键指标。延迟(Latency),流量(Traffic),错误(Errors)和饱和度(Saturation)。正如google sre 所讨论的,如果您只能衡量服务的四个指标,请关注这四个指标。 延迟Latency延迟是服务处理传入请求和发送响应所用时间的度量。测量服务延迟有助于及早发现服......
  • ABC297Ex - Diff Adjacent
    ABC297Ex-DiffAdjacent题目链接。\(\text{difficulty}=4.5,3\)。\(\text{tags}=多项式,生成函数,容斥\)。首先如果直接计数不相邻的那么至少需要记录当前的和以及最后一个数是什么,复杂度无法接受。那么考虑容斥。接下来对于一个固定的序列\(a_1,a_2,\dots,a_m\)考虑。......
  • 【剑指 Offer】 39. 数组中出现次数超过一半的数字
    【题目】数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 示例1:输入:[1,2,3,2,2,2,5,4,2]输出:2 限制:1<=数组长度<=50000 来源:力扣(LeetCode)链接:https://leetcode.cn/problems/shu-zu......
  • 【剑指 Offer】 56 - II. 数组中数字出现的次数 II
    【题目】在一个数组nums中除一个数字只出现一次之外,其他数字都出现了三次。请找出那个只出现一次的数字。示例1:输入:nums=[3,4,3,3]输出:4示例2:输入:nums=[9,1,7,9,7,9,7]输出:1 限制:   1<=nums.length<=10000   1<=nums[i]<2^31 来源:力扣(LeetCode)链接:http......