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 }