14378: 【原4378】金融操纵家
题目
题目描述
author: lei 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4378
Description
助教获得了一项“操纵”金融市场的神奇技能,只要他买入一支股票,这支股票就会下跌,只要他卖出一支股票,这支股票就会上涨。他悄咪咪的记录下来一支股票每天的价格。看到这些数据,他在想,如果没有他那神奇的技能,在一次买卖中他最多能赚多少呢?他最多只买卖一次,卖出一定要在买入之后。
Input Format
输入 第一行 整数n 表示有 n 个数字 第二行 n 个数字,用空格隔开
Output Format
输出 一个整形数字
Sample Input
6
7 1 5 3 6 4
Sample Output
5
解释:第二天买入价格为 1, 第五天卖出价格为 5,最多赚 5 元
Sample Input
5
7 6 4 3 1
Sample Output
0
解释:没有可以赚钱的交易,最多赚 0 元
Limits
80% 的数组长度小于 10^4 100% 的数组长度小于 10^6
ligongzzz's solution
//
// Created by 谢哲 on 2021/4/22.
//
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int cur_min = 999999999;
int result = 0;
int n;
cin >> n;
for(;n;--n) {
int a;
cin >> a;
result = max(result, a - cur_min);
cur_min = min(cur_min, a);
}
cout << result;
return 0;
}