The sequence of Noip simulated problem--mushroom

Source: Internet
Author: User

sequence of Mushroom
"Problem description"
Mushroom Hand has n number of rows in a row, now mushroom want to take a continuous sub-sequence, so that the sub-sequence satisfies: a maximum of only one number, so that the continuous subsequence is strictly ascending sub-sequence, mushroom want to know the longest length of this sequence is how much.
"input Format"
The first line is an integer n, which indicates the number of N.
The second behavior n number.
"Output format"
a number, for the longest length.
"Input Sample"
6
7 2 3 1 5 6
"output Example"
5
"Sample Interpretation"
Select the 2nd number to the 6th number, and change the 1 to a 4.
"Data range"
for 30% of data, n<=10
for 60% of data, n<=1000
for 100% of data, n<=100000

The first thing I thought of when I got the data was O (NLOGN) thought for a long time ...

And then I found this problem swept two times, OK.

First sweep, save the left and right position of the continuous increment interval

Second pass the scan directly to find the ANS: compared with the previous interval, because of the interval increment, so only to judge the pre-interval right-1 and the posterior interval of the left,and before the right and

After the interval of left+1 can be. (As for the increment interval of 1, add a special sentence to it)

Code:

1#include <cstdio>2#include <iostream>3#include <cstring>4#include <cmath>5 using namespacestd;6 Const intmaxn=100005;7 structnode{8     intLeft,right;9 }Q[MAXN];Ten inttemp=0; One intA[MAXN]; A intN; - intMain () - { theFreopen ("seq.in","R", stdin); -Freopen ("Seq.out","W", stdout); -scanf"%d",&n); -      for(intI=1; i<=n;i++) +scanf"%d",&a[i]); -q[++temp].left=1; +      for(intI=2; i<=n;i++) A     { at         if(a[i]>a[i-1])Continue; -         if(a[i]<=a[i-1]) -         { -q[temp].right=i-1; -temp++; -q[temp].left=i; in             Continue; -         } to     } +q[temp].right=N; -     intans=q[1].right-q[1].left+1; the      for(intI=2; i<=temp;i++) *     { $         if(q[i-1].left==q[i-1].right)//The front is a single point intervalPanax NotoginsengAns=max (ans,q[i].right-q[i-1].left+1); -         Else if(Q[i].left==q[i].right)//the back is a single point interval theAns=max (ans,q[i].right-q[i-1].left+1);  +         Else A         { the             if((a[q[i].left]>a[q[i-1].right-1]+1)|| (a[q[i].left+1]>a[q[i-1].right]+1)) +Ans=max (ans,q[i].right-q[i-1].left+1); -             Else $             { $Ans=max (ans,q[i-1].right-q[i-1].left+2); -Ans=max (ans,q[i].right-q[i].left+2); -             } the         }  -     }Wuyiprintf"%d", ans); the     return 0; -}

The sequence of Noip simulated problem--mushroom

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.