TJU训练赛—L

来源:互联网 发布:分区软件哪个好 编辑:程序博客网 时间:2024/05/16 15:31
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(ajai),0ijn1max(aj−ai),0≤i≤j≤n−1.

Input

The input consists of multiple test cases. The first line contains an integer TT, indicating the number of test cases.(1T1000)(1≤T≤1000)
Each case contains one integer NN.(0N107)(0≤N≤107). Then comes a line with N intergers ai(107ai107)ai(−107≤ai≤107)

Output

For each case only output the answer. 

Sample Input

151 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;} 


原创粉丝点击