Luogu P1420 longest connection number and p1420 longest connection number

Source: Internet
Author: User

Luogu P1420 longest connection number and p1420 longest connection number
Description

Enter n positive integers (1 <= n <= 10000). The maximum length of the serial number is required. (Serial numbers refer to continuous natural numbers from small to large)

Input/Output Format

Input Format:

 

The first line is a number n;

The second line contains n positive integers separated by spaces.

 

Output Format:

 

The maximum number of connections.

 

Input and Output sample input sample #1:
103 5 6 2 3 4 5 6 8 9           
Output sample #1:
5

For the first time, I had to score 20 points ..
I feel that I need to think about 233333 of my life.

Do not forget to set now to 1 when the if statement is invalid.

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #define ls k<<1 6 #define rs k<<1|1 7 using namespace std; 8 const int MAXN=400400; 9 inline void read(int &n)10 {11     char c=getchar();n=0;bool flag=0;12     while(c<'0'||c>'9')    c=='-'?flag=1,c=getchar():c=getchar();13     while(c>='0'&&c<='9')    n=n*10+c-48,c=getchar();flag==1?n=-n:n=n;14 }15 int a[MAXN];16 int main()17 {18     int n;19     read(n);20     for(int i=1;i<=n;i++)    read(a[i]);21     int now=1;22     int ans=1;23     for(int i=1;i<=n-1;i++)24     {25         if(a[i+1]-a[i]!=1)26             ans=max(ans,now),now=1;27         else now++;28     }29     printf("%d",ans);30     return 0;31 }

 

 

Related Article

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.