A. Kefa and First Steps(求最大连续递增的子数列的元素个数)

A. Kefa and First Steps

Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th day (1 ≤ i ≤ n) he makes ai money. Kefa loves progress, that’s why he wants to know the length of the maximum non-decreasing subsegment in sequence ai. Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order.
Help Kefa cope with this task!

Input

The first line contains integer n (1 ≤ n ≤ 105).

The second line contains n integers a1,  a2,  …,  an (1 ≤ ai ≤ 109).

Output

Print a single integer — the length of the maximum non-decreasing subsegment of sequence a.

Examples

Input
6
2 2 1 3 4 1
Output
3
Input
3
2 2 9
Output
3
Note
In the first test the maximum non-decreasing subsegment is the numbers from the third to the fifth one.

In the second test the maximum non-decreasing subsegment is the numbers from the first to the third one.

分析一下下:
看得懂题目的话其实很简单的,其实就是求一组数字里面最大连续递增的子数列的元素个数的最大值,无奈我做了好久都超时
一个VJ上“Runtime error on test 10”的一个代码:

#include <iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n, k[10000];
	while (scanf("%d", &n)!=EOF)
	{
		int ans =0, t = 0;
		for (int i = 0;i < n;i++)
			cin >> k[i];
		for (int i = 1;i < n;i++)
			if (k[i] >= k[i - 1])
			{
				t++;
				if (ans <= t) 
					ans =t;
			}
			else t = 0;
		cout << ans + 1 << endl;
	}
}

再看一个VJ上通过了的代码:

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	int k[100000], n, i, flag = 1, t = 1;
	cin >> n;
	for (i = 0;i < n;i++)
		cin >> k[i];
	for (i = 1;i < n;i++)
	{
		if (k[i] >= k[i - 1])
		{
			flag++;
		}
		else
			flag = 1;
		if (flag > t)
			t = flag;
	}
	cout << t << endl;
}

Status
Accepted
Time
93ms
Memory
400kB
Length
306
Lang
Microsoft Visual C++ 2010
Submitted
2019-01-18 22:46:54

猜你喜欢

转载自blog.csdn.net/weixin_43697280/article/details/86547628