当前位置:网站首页>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;
}
边栏推荐
- JS inheritance
- idea灰屏问题
- Node red series (28): communication with Siemens PLC based on OPC UA node
- Thingsboard tutorial (II and III): calculating the temperature difference between two devices in a regular chain
- 数据链路层详解
- Daily summary of code knowledge
- FortiGate firewall configuration link detection link monitor and status query
- JS generator
- MySQL updates JSON string in array form
- 基于SSM框架茶叶商城系统【项目源码+数据库脚本+报告】
猜你喜欢

el-upload上传文件(手动上传,自动上传,上传进度)

Educoder group purchase suspension box page production

两个月拿到N个offer,什么难搞的面试官在我这里都不算事

基于海康EhomeDemo工具排查公网部署出现的视频播放异常问题

Basic knowledge of redis

《机器人SLAM导航核心技术与实战》第1季:第0章_SLAM发展综述

How to solve the problem of link hyperlinks when trying to link the database?

Slam mapping, automatic navigation and obstacle avoidance based on ROS (bingda robot)

I spent three years in a big factory outsourcing, which subverted my understanding!

OneNote software
随机推荐
Redis cache avalanche, breakdown and penetration
OneNote software
Qt6 QML Book/Qt Quick 3D/Qt Quick 3D
el-upload上传文件(手动上传,自动上传,上传进度)
Junior students summarize JS basic interview questions
Qt Creator 8 Beta2发布
JS proxy
FortiGate performs DNAT mapping, and intranet users cannot normally access the mapping
Detailed explanation of data link layer
Memorize unfamiliar words at SSM stage and update them from time to time
Node red series (28): communication with Siemens PLC based on OPC UA node
An error occurs when sqlyog imports the database. Please help solve it!
Indefinite parameters of JS function
SQL追加字段
Es2016 key summary
JS static method
《机器人SLAM导航核心技术与实战》第1季:第0章_SLAM发展综述
Code cloud fatal: authentication failed for
Educoder group purchase suspension box page production
Blue Bridge Cup: magic cube rotation [Vocational group]