一开始没有想到简单的推理方法。
最简单的是在当前点刷新diff的值,然后更新最大的Ai

c#include <cstdio>
#include <algorithm>

using namespace std;

#define REP(i, n) for(int i = 0; i < (n); i++)

const int maxn = 100000 + 10;
int a[maxn];

int n;
int main()
{
    freopen("input", "r", stdin);
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        REP(i, n)
            scanf("%d", &a[i]);
        int diff = a[0] - a[1];
        int MaxAi = a[0];
        for(int j = 2; j < n; j++)
        {
            diff = max(diff, MaxAi - a[j]);
            MaxAi = max(MaxAi, a[j]);
        }
        printf("%d\n", diff);
    }
    return 0;
}


svtter
209 声望37 粉丝

更喜欢原型开发