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);
}
推荐阅读
-
Maximum Profit
-
svn number is larger than maximum 博客分类: 数据库
-
53. Maximum Subarray
-
change delicate server max player numbers HOW-TOs 博客分类: UnrealEngine4 what is the maximum number of players UE4 can handle?
-
(2020.1.2已解决)pyinstaller生成exe出错:RecursionError: maximum recursion depth exceeded
-
Codeforces 888E Maximum Subsequence(折半搜索)
-
[CodeForces 888E] Maximum Subsequence
-
RMQ (Range Minimum/Maximum Query)算法---ST算法
-
hadoop中mapred.tasktracker.map.tasks.maximum的设置 博客分类: hadoop精选
-
杭电2018暑假多校联赛第一场的第一题 Maximum Multiple