欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Maximum Profit

程序员文章站 2024-03-22 23:09:10
...

Maximum Profit
You can obtain profits from foreign exchange margin transactions. For example, if you buy 1000 dollar at a rate of 100 yen per dollar, and sell them at a rate of 108 yen per dollar, you can obtain (108 - 100) × 1000 = 8000 yen.

Write a program which reads values of a currency Rt at a certain time t (t=0,1,2,…n−1), and reports the maximum value of Rj−Ri where j>i .

Input
The first line contains an integer n. In the following n lines, Rt (t=0,1,2,…n−1) are given in order.

Output
Print the maximum value in a line.

Constraints
2≤n≤200,000
1≤Rt≤109
Sample Input 1
6
5
3
1
3
4
3
Sample Output 1
3
Sample Input 2
3
4
3
2
Sample Output 2
-1


#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int main() {
    int n, t;
    int maxi, mini;
    scanf("%d", &n);
    scanf("%d", &t);
    maxi = -1000000001; mini = t;
    for (int i = 1; i < n; i++) {
        scanf("%d", &t);
        // 先更新当前的最大值
        maxi = maxi < t - mini ? t - mini : maxi;
        // 再更新当前最小值
        mini= mini > t ? t : mini;
    }
    printf("%d\n", maxi);
}