Maximum Profit
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的更多相关文章
- [Educational Round 59][Codeforces 1107G. Vasya and Maximum Profit]
咸鱼了好久...出来冒个泡_(:з」∠)_ 题目连接:1107G - Vasya and Maximum Profit 题目大意:给出\(n,a\)以及长度为\(n\)的数组\(c_i\)和长度为\( ...
- Maximum profit of stocks
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- Yaoge’s maximum profit HDU - 5052
Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈
Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...
- 【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 ...
- HDU5052 Yaoge’s maximum profit(LCT)
典型的LCT操作,但是维护的是一个序列最左边减最右边的最小值,所以要维护左边减右边的最小值del[0]和一个右边减左边的最小值del[1](因为rev标记swap的时候对应的值也要交换).维护的时候d ...
- HDU 5052 Yaoge’s maximum profit 光秃秃的树链拆分 2014 ACM/ICPC Asia Regional Shanghai Online
意甲冠军: 特定n小点的树权. 以下n每一行给出了正确的一点点来表达一个销售点每只鸡价格的格 以下n-1行给出了树的侧 以下Q操作 Q行 u, v, val 从u走v,程中能够买一个鸡腿,然后到后面卖 ...
- codeforces1107G Vasya and Maximum Profit 【模拟】
题目分析: 前缀和啥的模拟一下就行了. 代码: #include<bits/stdc++.h> using namespace std; ; int n,x,d[maxn],sta[max ...
- Codeforces 1107G Vasya and Maximum Profit [单调栈]
洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...
随机推荐
- 2019.03.26 读书笔记 关于for与foreach
for 是索引器,foreach是迭代器 foreach在movenext()中增加了对集合版本(一个整数,每次对集合修改都+1)的验证,另外反编译后的效果是使用了using(是try finally ...
- linux无敌权限之chattr
chattr 修改文件或者目录的文件属性,添加超级权限 -R 递归修改文件机子目录 -V 显示详细修改的内容 + 在原有的基础上追加参数 — 减参数 = 跟新为指定参数 a append 设定改参数后 ...
- python 爬虫系列05--丑事百科
丑事百科爬虫 import re import requests def parse_page(url): headers = { 'User-Agent':'user-agent: Mozilla/ ...
- Python基本数据类型和其常用方法
Number Bool String List Tuple Dictionary Number 在Python3中 所有整数不管多大都是Int类型,没有Long类型,Python2中有Long类型. ...
- TOJ 4003 Next Permutation
描述 It is an interesting exercise to write a program to print out all permutations of 1, 2, …, n. How ...
- 给python解释器本身添加注册表
import sys from _winreg import * # tweak as necessary version = sys.version[:3] installpath = sys.pr ...
- 深入学习keepalived之一 keepalived的启动
1.keepalived的启动过程: 启动健康检查子进程和vrrp子进程.其中_WITH_LVS_,_WITH_VRRP_在configure和configure.in文件中定义. 源码如下: /* ...
- ICONIX
- OpenLayers3 实现自定义放大缩小滑块,自定义方向按钮
由于项目需要,需要自定义滑块.为此记录如下: <div id="map" class = "map"> <div id = "zoo ...
- JSON.stringify使用
基本使用 JSON.stringify(value[, replacer [, space]]) value 将要序列化成 一个JSON 字符串的值. replacer 可选 如果该参数是一个函数,则 ...