当前位置:网站首页>Equity interest [non DP]
Equity interest [non DP]
2022-06-30 04:23:00 【MC happy bitter Xiao afraid】
Title Description :
Title Description
Xiao Ke's father is interested in studying the stock market recently , Xiao Ke thinks naively , If you can predict the daily price of stocks , It's easy to figure out when to buy and when to sell to maximize profits . Suppose that the time sequence of a stock is given n Sky price , Excuse me at n What is the maximum profit that can be made by buying and selling the stock only once in a day ?
Input format
first line , An integer n, Days ,1≤n≤10^5
Next line ,n It's an integer , The first i It's an integer , It means the first one i Day's share price . All integers are 1 To 10000 Positive integers within
Output format
Input and output sample Columns
sample input 1:
6
7 1 5 3 6 4
sample output 1:
5
sample input 2:
5
7 6 4 3 1
sample output 2:
0
explain
【 Sample explanation 1】 In the 2 God ( Stock price = 1) Buy when , In the 5 God ( Stock price = 6) Sell when , Maximum profit = 6-1 = 5 .
Note that profit cannot be 7-1 = 6, Because you can only buy first and then sell
【 Sample explanation 2】 There is no need to trade in this case , In this way, the profit can be maximized 0
It's a little bit easier , about a[i], stay 1 ~ i - 1 In the process of finding a[1] To a[i - 1] Minimum of , With the a[i] Subtracting the , take max, But pay attention to the special judgment < 0 The situation of :
#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;
}
边栏推荐
猜你喜欢

FortiGate firewall and Aruze cloud tunnel interruption

AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里

lego_ Reading and summary of loam code

Error encountered in SQL statement, solve

Myrpc version 6

Named pipes for interprocess communication

OneNote production schedule

在大厂外包呆了三年,颠覆了我的认知!

Myrpc version 3

什么是光耦电路,实际使用中应该注意些什么?
随机推荐
Basic knowledge of redis
SQL server2005中SUM函数中条件筛选(IF)语法报错
Huawei cloud native - data development and datafactory
JS import and export
尝试链接数据库时出现链接超时报错,如何解决?
The school training needs to make a registration page. It needs to open the database and save the contents entered on the registration page into the database
base64.c
Qt 6.3.1Conan软件包发布
找到接口在表单里加参数
FortiGate configures multiple server IPS as link monitor monitoring objects on the same interface
Threejs实现模拟河流,水面水流,水管水流,海面
Splicing strings with custom functions
Anonymous pipeline for interprocess communication
Technology sharing | broadcast function design in integrated dispatching
Blue Bridge Cup: magic cube rotation [Vocational group]
[image fusion] multi focus and multi spectral image fusion based on cross bilateral filter and weighted average with matlab code
Error encountered in SQL statement, solve
SQL append field
If you encounter problems when using spark for the first time, please ask for help
Configure specific source IP in SLA detection of FortiGate sdwan