输入样例:
5
1 2 1 4 6
6
10 8 8 6 4 3
输出样例:
5
0
C++ 代码
#include <iostream>
using namespace std;
const int N = 1010;
int n,res,s[N];
int main()
{
while(scanf("%d",&n) != -1)
{
res = 0;
for(int i = 0;i<n;i++) scanf("%d",&s[i]);
for(int i = 0;i<n;i++)
{
int j = i + 1;
while(j < n && s[j] >= s[j-1]) j++;
res = max(res,s[j-1] - s[i]);
}
printf("%d\n",res);
}
return 0;
}
只算连续升序的序列,s[j-1]表示单个连续子序列中最大的值,然后再不断更新最大值。