【Aizu ALDS1_1_D --- Maximum Profit】
Description
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.
2≤n≤200,000
1≤Rt≤109
Output
Print the maximum value in a line.
Sample Input
6
5
3
1
3
4
3
Sample Output
3
AC代码:
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 200010
int arr[MAXN];
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i=0;i<n;i++)
cin >> arr[i];
int minn = arr[0];
int maxx = -0x3f3f3f3f;
for(int i=1;i<n;i++)
{
maxx=max(maxx,arr[i]-minn);
minn=min(arr[i],minn);
}
cout << maxx << endl;
return 0;
}