Description
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 sequenceai. 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.
Sample Input
6 2 2 1 3 4 1
3
3 2 2 9
3
Hint
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.
水题:求连续的最长非递减子序列 就是注意数据范围,最后的结果用 long long , int 会出错。我做这道题的时候, 最开始maxn, sum.都初始化为0, 结果挂了, 后来一想,无论怎样都应该初始化为1,
以后细心点,把问题都考虑全了在做题, 别挂了才开始思考。
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[100002];
int main(){
int n, i;
long long int sum = 0, maxn = 0;
while(~scanf("%d", &n)){
for(i = 0; i < n; ++i)
scanf("%d", &a[i]);
sum = 1;
maxn = 1;
for(i = 1; i < n; ++i){
if(a[i] >= a[i - 1]){
++sum;
maxn = max(sum, maxn);
}
else {
sum = 1;
}
}
cout << maxn << endl;
}
return 0;
}
本文介绍了一种解决寻找序列中最长非递减子序列问题的方法,通过一次遍历即可得出答案,适用于序列长度可达10^5的情况。
236

被折叠的 条评论
为什么被折叠?



