最长连续不下降序列

WUST:1001: 最长连续不下降序列
Time Limit: 1 Sec Memory Limit: 128 MB 64bit IO Format: %lld
Submitted: 1053 Accepted: 196
[Submit][Status][Web Board]
Description

给n个数字,连续的、依照不降次序排列的数字可以构成一个数字序列。求最长的数字序列的长度。

Input

多组数据。

对于每组数据:

第一行输入n (1≤n≤100000).
第二行输入n个数字,a1,a2,…,an (1≤ai≤1000000000).

Output

输出最长的数字序列的长度。

Sample Input

6
2 2 1 3 4 1
3
2 2 9

Sample Output

3
3

代码:

#include
int main()
{
    int n,i,cnt;
    while(scanf("%d",&n)!=EOF)
    {
        cnt=1;
        int temp=1,flag=0,x=0,a,b;
        scanf("%d",&a);
        for(i=1;i//边输入边计算
        {
          scanf("%d",&b);
          if(a<=b)
          {
              temp++;
              if(temp>cnt)//temp为临时变量保存当前升序序列中元素个数
                cnt=temp;//cnt保存目前为止序列长度的最大值
          }
          else
            temp=1;
            a=b;//a中保存上一个数值,等待与下一个值比较
         }
        printf("%d\n",cnt);
    }
    return 0;
}

这里写代码片

你可能感兴趣的:(最长连续不下降序列)