Description
We are given a integer sequence, your job is find the length of the longest contiguous subsequence that is strictly increasing or strictly decreasing.
Input
- First number , represent how many test cases.
- For each test case the first number is .
- Then positive integers are followed, all of them are less than 101.
Output
For each test case output the answer in one line.
Sample Input
3
3 1 1 1
3 1 2 3
4 4 3 2 1
Sample Output
1 3 4
思路: 用2个数组存取递增递减序列,并且浪费点空间节省效率的方法去做。
1 #include<stdio.h> 2 int main() 3 { 4 int i,j,test,num,len,A[ 50 ],B[ 50 ],C[ 50 ]; 5 scanf( " %d " ,& test); 6 for (i= 0 ;i<test;i++ ) 7 { 8 scanf( " %d " ,& num); 9 for (j= 0 ;j<num;j++ ) 10 scanf( " %d " ,& C[j]); 11 A[ 0 ]= 1 ; 12 B[ 0 ]= 1 ; 13 for (j= 1 ;j<num;j++ ) 14 { 15 if (C[j]>C[j- 1 ]) 16 { 17 A[j]=A[j- 1 ]+ 1 ; 18 B[j]= 1 ; 19 } 20 if (C[j]<C[j- 1 ]) 21 { 22 A[j]= 1 ; 23 B[j]=B[j- 1 ]+ 1 ; 24 } 25 if (C[j]==C[j- 1 ]){ 26 A[j]= 1 ; 27 B[j]= 1 ; 28 } 29 } 30 for (j= 0 ,len= 0 ;j<num;j++ ) 31 { 32 if (A[j]>len) len= A[j]; 33 if (B[j]>len) len= B[j]; 34 } 35 printf( " %d\n " ,len); 36 } 37 return 0 ; 38 }