Open Credit System UVA

来源:互联网 发布:积分商城java源代码 编辑:程序博客网 时间:2024/05/21 17:17

因为i总是小于j的,所以扫描一遍,就可以直到j前面的最大值,然后用此最大值减去当前值(j)来更新答案

#include<cstdio>#include<cstring>using namespace std;int main() {    int t, n, min, max, ans;    scanf("%d", &t);    while(t--) {        scanf("%d", &n);        int ans, a, b, x, maxi;        scanf("%d%d", &a, &b);  //读取数组前两个数        ans = a - b;            //初始答案        maxi = a > b ? a : b;   //初始最大值        for(int j = 2; j < n; ++j) {        //边扫边维护ans,maxi            scanf("%d", &x);            ans = ans > (maxi - x) ? ans : (maxi - x);      //更新答案i < j            maxi = maxi > x ? maxi : x;                     //更新到当前位置最大的i        }        printf("%d\n",ans);    }    return 0;}


原创粉丝点击