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]\)时那个奇怪东西的 ...
随机推荐
- Unity运用GPU代替CPU处理和计算简单测试
http://www.manew.com/thread-110502-1-1.html 随着游戏玩法的增强,计算的多量化,我们的CPU并不足以迅速的处理这些问题,而Unity给我们开放了一个接口,我们 ...
- 移动端或APP禁止放大标识
如果手机端或者APP的应用里面,有点击一下屏幕会自己放大,解决办法如下: 在头部添加一条meta标识 <meta name="viewport" content=" ...
- Nginx+Keepalived配置
1. Nginx安装 (1) 环境:分别在2台服务器上部署nginx且步骤一致: 如192.138.86.1和192.138.86.2 (2) 下载官网最新稳定版,地址:https://nginx.o ...
- 利用request、beautifulsoup、xml写多线程爬虫
# -*- coding:UTF-8 -*- import requests,time from collections import OrderedDict import threading fro ...
- [转]JS跨域解决方式 window.name
本文转自:http://www.cnblogs.com/lichuntian/p/4909465.html window.name 传输技术,原本是 Thomas Frank 用于解决 cookie ...
- sqlServer游标的使用
USE [PatPD1]GO/****** Object: UserDefinedFunction [dbo].[fun_GetConditionInner] Script Date: 201 ...
- java基础--常用函数总结
java基础--常用函数总结 2019-3-16-23:28:01-----云林原创 1.split()字符串分割函数 将一个字符串分割为子字符串,然后将结果作为字符串数组返回. 2.Math.flo ...
- VS2015自定义类模板的方法
在前一段时间忽然想给自己电脑上的vs新建类的时候添加一个自定义个注释,但是在网上搜了很久都是说vs2012之类的方法系统也都是win7.XP之类的独独没有win8的.故此自己不断的尝试修改发现方法如下 ...
- [SQL SERVER系列]工作经常使用的SQL整理,实战篇(三)[原创]
工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实战篇(三) 接着本系列前面两篇继续讨论. 有时 ...
- 同源策略和Jsonp、CORS
一.同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web是构建在同源策略基础之 ...