当前位置:网站首页>股票利益【非dp】
股票利益【非dp】
2022-06-30 04:22:00 【MC快乐的苦Xiao怕】
题目描述:
题目描述
小科的爸爸最近热衷于研究股市,小科天真的想,如果能够预知股票每天的价格,那么就很容易计算出什么时候买入什么时候卖出利润才能最大化。假设按照时间先后顺序给出某股票的n天的价格,请问在n天内只能买卖该股票一次可能获得的最大利润是多少?
输入格式
第一行,一个整数n,表示天数,1≤n≤10^5
接下来一行,n个整数,第i个整数,表示第i天的股票价格。整数都是1到10000以内的正整数
输出格式
输入输出样列
输入样例1:
6
7 1 5 3 6 4
输出样例1:
5
输入样例2:
5
7 6 4 3 1
输出样例2:
0
说明
【样例说明1】在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为只能先买后卖
【样例说明2】这种情况下不需要进行交易,这样利润才能最大是0
还是比较简单的,对于a[i],在1 ~ i - 1的过程中求出a[1]到a[i - 1]中的最小值,在与a[i]相减,取max,但是要注意特判 < 0的情况:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <map>
using namespace std;
const int N = 100010 ;
int a[N] , n ;
int minn , maxn , c ;
int main()
{
cin >> n ;
if (n < 2)
{
cout << 0 ;
return 0 ;
}
for (int i = 1; i <= n; i++) cin >> a[i] ;
minn = a[1] , maxn = a[2] - a[1] ;
for (int i = 3; i <= n; i++)
{
if (a[i - 1] < minn)minn = a[i - 1] ;
c = a[i] - minn ;
maxn = max (maxn , c) ;
}
cout << max (0 , maxn) ;
return 0;
}
边栏推荐
- Internship: interface case implementation
- Named pipes for interprocess communication
- Explain the underlying principles of JVM garbage collection in simple terms
- [cloud native] AI cloud development platform - Introduction to AI model foundry (developers can experience AI training model for free)
- Day 10 data saving and loading
- JS reflect
- El upload upload file (manual upload, automatic upload, upload progress)
- 7-3 打怪升级 单源最短路
- You know AI, database and computer system
- OneNote production schedule
猜你喜欢

第九天 脚本与资源管理

Grasp grpc communication framework in simple terms

You know AI, database and computer system

基于servlet+jsp+mysql实现的工资管理系统【源码+数据库】

Matlab reads fig file and restores signal
![[fuzzy neural network prediction] water quality prediction based on fuzzy neural network, including Matlab source code](/img/88/038826ec6d16c8eb04d9ef2e01d47a.png)
[fuzzy neural network prediction] water quality prediction based on fuzzy neural network, including Matlab source code

FortiGate firewall quick initialization administrator password

Myrpc version 2
![[image fusion] multi focus and multi spectral image fusion based on cross bilateral filter and weighted average with matlab code](/img/9c/2553d192c2f9b93acc6550220c447f.png)
[image fusion] multi focus and multi spectral image fusion based on cross bilateral filter and weighted average with matlab code

I get n offers in two months. I don't have any difficult interviewers here
随机推荐
About manipulator on Intelligent Vision Group
Technology sharing | broadcast function design in integrated dispatching
Modifier of JS regular expression
工程安全和工程质量
Es2016 key summary
How to solve the problem of link hyperlinks when trying to link the database?
Concatenation of Languages(UVA10887)
Named pipes for interprocess communication
Ora-00907: missing right parenthesis problem supplement
Everyone, Flink 1.13.6, mysql-cdc2.2.0, the datetime (6) class extracted
Troubleshoot abnormal video playback problems in public network deployment based on Haikang ehomedemo tool
Redis sentry, persistence, master-slave, hand tear LRU
SQL append field
Project safety and quality
Error encountered in SQL statement, solve
Implementation steps of dynamic proxy
Qt6 QML Book/Qt Quick 3D/Qt Quick 3D
SQLyog导入数据库时报错,求帮解决!
两个月拿到N个offer,什么难搞的面试官在我这里都不算事
DBT product initial experience