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 RtRt at a certain time tt (t=0,1,2,...n−1t=0,1,2,...n−1), and reports the maximum value of Rj−RiRj−Ri where j>ij>i .

Input

The first line contains an integer nn. In the following nn lines, RtRt (t=0,1,2,...n−1t=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

一开始想到两重循环
for(int j = 1; j < n; ++ j)
  for(int i = 0; i < j; ++ i)
    maxn = max(maxn , a[j] - a[i]); 但是 n≤200,000 若采用两重循环(O(n^2))会超时, 所以在i自增的过程中, 将现阶段a[i]的最小值(记为minn)保存下来, 此时只需要O(1)便可求出i时刻的最大利益
for(int i = 1; i < n; ++ i)
{
  maxn = maxn(a[i] - minn);  // minn初始化为a[0]
  minn = min(minn, a[i]);
}
注意 maxn的初始值不能是-1, 因为如果序列单调递减, 则最大值有可能小于-1(-1反而比该序列的maxn还大)
方便起见, maxn初值为a[1] - a[0] 边扫描边记录
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 200010;
int a[MAX];
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; ++ i)
{
cin >> a[i];
} int maxn = a[1] - a[0], minn = a[0]; for(int i = 1; i < n; ++ i)
{
maxn = max(maxn, a[i] - minn);
minn = min(minn, a[i]);
}
cout << maxn << endl;
return 0;
}

  

Maximum Profit的更多相关文章

  1. [Educational Round 59][Codeforces 1107G. Vasya and Maximum Profit]

    咸鱼了好久...出来冒个泡_(:з」∠)_ 题目连接:1107G - Vasya and Maximum Profit 题目大意:给出\(n,a\)以及长度为\(n\)的数组\(c_i\)和长度为\( ...

  2. Maximum profit of stocks

    https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...

  3. Yaoge’s maximum profit HDU - 5052

    Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  4. Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈

    Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...

  5. 【leetcode】1235. Maximum Profit in Job Scheduling

    题目如下: We have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtai ...

  6. HDU5052 Yaoge’s maximum profit(LCT)

    典型的LCT操作,但是维护的是一个序列最左边减最右边的最小值,所以要维护左边减右边的最小值del[0]和一个右边减左边的最小值del[1](因为rev标记swap的时候对应的值也要交换).维护的时候d ...

  7. HDU 5052 Yaoge’s maximum profit 光秃秃的树链拆分 2014 ACM/ICPC Asia Regional Shanghai Online

    意甲冠军: 特定n小点的树权. 以下n每一行给出了正确的一点点来表达一个销售点每只鸡价格的格 以下n-1行给出了树的侧 以下Q操作 Q行 u, v, val 从u走v,程中能够买一个鸡腿,然后到后面卖 ...

  8. codeforces1107G Vasya and Maximum Profit 【模拟】

    题目分析: 前缀和啥的模拟一下就行了. 代码: #include<bits/stdc++.h> using namespace std; ; int n,x,d[maxn],sta[max ...

  9. Codeforces 1107G Vasya and Maximum Profit [单调栈]

    洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...

随机推荐

  1. Quartz.NET 作业调度使用

    Quartz.NET的使用方法有很多,今天使用Quartz.NET3.0.6的时候发现和2.0版本的语法不太一样,百度上找了一圈也没有找到解决办法 后来在GitHub上下载源代码解决了 实现每隔10s ...

  2. js动画实现&&回调地狱&&promise

    1. js实现动画 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  3. 设置ibus为默认输入法

    /etc/profile  文件中编辑 export INPUT_METHOD="ibus"

  4. 【Linux】Linux下使用Docker快速部署Oracle数据库

    安装最新Docker wget -qO- https://get.docker.com/ | sh` 安装 docker-compose sudo curl -L https://github.com ...

  5. centOS查看apache版本的命令

    在centOS 7下: 命令如下: httpd -v

  6. 数据挖掘:提取百度知道QA中的影视信息

    1. 背景 网站上爬取了部分关于影视的百度知道QA,为了后续提高影视的搜索效果,需要基于百度知道QA的答案抽取相关的影视信息. 2. 流程 目前已有基础的媒资视频库信息,基于媒资视频库中的视频名称,构 ...

  7. gcc链接非标准(non-standard)命名库

    标准命名库: -lnamespace 标准链接库以lib开头, 并以so/a结尾. example gcc test.c -o test -L. -lhello 非标准命名库: -l:libname ...

  8. bootstrap清除数据源

    下拉框使用动态数据源,当下拉框触发change事件时,想让下拉框改变数据源,加了个if判断 $('#@idForCostCategory').change(function (event) { if( ...

  9. JavaScript对象 原型

    javascript对象就是一组数据和功能的集合,除原始类型(string.number.boolean.null.undefined)之外,其余都是对象. 可以通过对象直接量(字面量).new.和O ...

  10. win10下MySQL 5.7.20解压版安装步骤

    1.从官网下载MySQL5.7.20解压版64位:https://dev.mysql.com/downloads/file/?id=473309. 2.解压(我的解压路径为:E:\mysql-5.7. ...