zoukankan      html  css  js  c++  java
  • DZY Loves Sequences

    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    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

    Input
    6
    7 2 3 1 5 6
    Output
    5

    Hint

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

    代码有毒! 调了好久好久。

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 int main()
     6 {
     7     int i,j;
     8     int b[100005],c[100005];
     9     int n,a[100005],ma;
    10     while(scanf("%d",&n)!=EOF)
    11     {
    12         memset(b,0,sizeof(b));
    13         memset(a,0,sizeof(a));
    14         memset(c,0,sizeof(c));
    15         for(i=1;i<=n;i++)
    16             scanf("%d",&a[i]);
    17         ma=0;
    18         a[0]=0,a[n+1]=0,b[0]=0,b[n+1]=0,c[0]=0,c[n+1]=0;
    19         for(i=1;i<=n;i++)
    20         {
    21             if(a[i]>a[i-1])
    22                 b[i]=b[i-1]+1;
    23             else
    24                 b[i]=1;
    25             if(b[i]>ma)
    26                 ma=b[i];
    27         }
    28         for(i=n;i>=1;i--)
    29         {
    30             if(a[i]<a[i+1])
    31                 c[i]=c[i+1]+1;
    32             else
    33                 c[i]=1;
    34             if(c[i]>ma)
    35                 ma=c[i];
    36         }
    37         for(i=1;i<=n;i++)
    38         {
    39             if(a[i+1]-a[i-1]>=2)
    40             {
    41                 ma=max(ma,b[i-1]+c[i+1]+1);
    42             }
    43             else
    44             {
    45                 ma=max(ma,max(b[i-1]+1,c[i+1]+1));
    46             }
    47         }
    48         printf("%d
    ",ma);
    49     }
    50     return 0;
    51 }
    View Code
  • 相关阅读:
    MySQL的数据库,数据表,数据的操作
    数组函数
    字符串函数,时间函数,数学函数,数组
    PHP函数
    php类型的相关函数,运算符,条件判断,循环
    PHP数据类型
    vector中erase用法注意事项
    C++11 右值引用&&
    vector中find和find_if的用法 以后再遍历剁手!
    在cocos2d中添加自己的shader教程
  • 原文地址:https://www.cnblogs.com/cyd308/p/4651657.html
Copyright © 2011-2022 走看看