SZU : A11 Sequence

本文讨论了一个算法问题,即在一个整数序列中找到最长的严格递增或递减连续子序列。通过使用两个数组来存储递增和递减序列,本文提供了一种节省内存并提高效率的解决方案。

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 T (1\leq T \leq 100), represent how many test cases.
  • For each test case the first number is N (1\leq N \leq 50).
  • Then N\, 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 }

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值