UVA-11078 Open Credit System

Input
Input consists of a number of test cases T (less than 20). Each case starts with an integer n which is
the number of students in the course. This value can be as large as 100,000 and as low as 2. Next n
lines contain n integers where the i'th integer is the score of the i'th student. All these integers have
absolute values less than 150000. If i < j, then i'th student is senior to the j'th student.
Output
For each test case, output the desired number in a new line. Follow the format shown in sample
input-output section.
Sample Input
3
2
100
20
4
4
3
2
1
4
1
2
3
4
Sample Output
80
3
-1

开始想着分为前缀最大值和后缀最大值俩数组,求差的最大和。后来发现可以动态的更新,省掉了数组的开销

#include <cstdio>
#include <algorithm>

using namespace std;

int A[100000];

int main() {
    int T, n;
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        for (int i = 0; i < n; ++i) {
            scanf("%d", A + i);
        }
        int t = A[0], ans = A[0] - A[1];
        for (int i = 1; i < n; ++i) { //二者同时更新,最大结果值,和最大索引
            ans = max(ans, t - A[i]);
            t = max(t, A[i]);
        }
        printf("%d\n", ans);
    }
}

 

posted @ 2017-10-04 22:40  少年啦飞驰  阅读(101)  评论(0编辑  收藏  举报