4171. L-The math problem
Time Limit: 1.0 Seconds
Memory Limit: 65536K
Total Runs: 286
Accepted Runs: 86
Given an array a with n intergers, please tell me the max(aj−ai),0≤i≤j≤n−1 max(aj−ai),0≤i≤j≤n−1.
Input
The input consists of multiple test cases. The first line contains an integer T
T, indicating the number of test cases.(1≤T≤1000)
(1≤T≤1000)
Each case contains one integer N
N.(0≤N≤107)
(0≤N≤107). Then comes a line with N intergers ai(−107≤ai≤107)
ai(−107≤ai≤107)
Output
For each case only output the answer.
Sample Input
1
5
1 3 5 4 2
Sample Output
4
【分析】
送分题....求这组序列中最大的a[j]-a[i],保证j>=i
因为j可以等于i所以最小值不会是负数,然后注意下只能用后面的数减去前面的数,所以不能求序列最大值和最小值减一下...
求一下1-i的最小值然后用当前读取的x-min跟ans判断一下求个max就好了
【代码】
#include <stdio.h>
int main()
{
int pp;scanf("%d",&pp);
while (pp--)
{
int n;scanf("%d",&n);
int now,x;scanf("%d",&now);
int ans=0;
for (int i=1;i<n;i++)
{
scanf("%d",&x);
if (x-now>ans) ans=x-now;
if (x<now) now=x;
}
printf("%d\n",ans);
}
return 0;
}