当前位置:网站首页>动态规划——股票买卖5
动态规划——股票买卖5
2022-07-27 13:29:00 【战士小小白】
股票买卖 V
给定一个长度为 N 的数组,数组中的第 i 个数字表示一个给定股票在第 i 天的价格。
设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
- 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
- 卖出股票后,你无法在第二天买入股票 (即冷冻期为 11 天)。
输入格式
第一行包含整数 N,表示数组长度。
第二行包含 N 个不超过 1000010000 的正整数,表示完整的数组。
输出格式
输出一个整数,表示最大利润。
数据范围
1≤N≤1051≤N≤105
输入样例:
5
1 2 3 0 2
输出样例:
3
样例解释
对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出],第一笔交易可得利润 2-1 = 1,第二笔交易可得利润 2-0 = 2,共得利润 1+2 = 3。
#include <bits/stdc++.h>
using namespace std;
const int N = 100010, INF = 0x3f3f3f3f;
int n;
int w[N];
int f[N][3];
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i ++) scanf("%d", &w[i]);
f[0][0] = f[0][1] = -INF;
f[0][2] = 0;
for(int i = 1; i <= n; i ++)
{
f[i][0] = max((f[i - 1][2] - w[i]), f[i - 1][0]);
f[i][1] = f[i - 1][0] + w[i];
f[i][2] = max(f[i - 1][1], f[i - 1][2]);
}
printf("%d\n", max(f[n][1], f[n][2]));
return 0;
}

边栏推荐
- 开源版思源怎么私有部署
- 线程知识总结
- 基于预训练模型的多标签专利分类研究
- Vscode -- create template file
- Rtl8762dk environment construction (I)
- 连接ResourceManager 失败
- Recursive method to realize the greatest common divisor
- this指向问题,闭包以及递归
- What you want most is the most comprehensive summary of C language knowledge. Don't hurry to learn
- 【医疗行业】DICOM converter Tools
猜你喜欢

Slam overview Reading Note 4: a survey on deep learning for localization and mapping: towards the age of spatial 2020

【科普】精度和分辨率的区别与联系

机场云商sign解析

大家最想要的,最全的C语言知识点总结,还不赶紧学习

基于在线问诊记录的抑郁症病患群组划分与特征分析

Lighting 5g in the lighthouse factory, Ningde era is the first to explore the way made in China

Cultural tourism and data collection | travel to Yunnan in an artistic way

@Bean 与 @Component 用在同一个类上,会发生什么?

NFT 的 10 种实际用途

Navicate reports an error access violation at address 00000000
随机推荐
基于招聘广告的岗位人才需求分析框架构建与实证研究
Import the virtual machine officially made by Kali Linux into Oracle VirtualBox
DVWA全级别通关教程
2022牛客多校二_ E I
[cache series] completely solve the problems of cache avalanche, breakdown and penetration
Detoxify! After Harbin Institute of technology was disabled MATLAB, domestic industrial software fought back domineering
面向不平衡数据的电子病历自动分类研究
Getting started for beginners: build your own blog with WordPress
The difference between [x for X in list_a if not np.isnan (x)] and [x if not np.isnan (x) else none for X in list_a]
这年头谁还不会抓包,WireShark 抓包及常用协议分析送给你!
Docker实践经验:Docker 上部署 mysql8 主从复制
va_ List usage summary
Failed to connect to ResourceManager
Blocking queue
架构——MVC的升华
Chapter 3 business function development (add clues and remarks, and automatically refresh the added content)
LeetCode·每日一题·592.分数加减运算·模拟
[idea] set to extract serialVersionUID
解气!哈工大被禁用MATLAB后,国产工业软件霸气回击
How to make computers have public IP