当前位置:网站首页>动态规划——股票买卖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;
}

边栏推荐
- 网上券商APP开户安全有保障吗?
- How to view revenue and expenditure by bookkeeping software
- Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
- STM32——电容触摸按键实验
- log4j2 jdbc appender
- 连接ResourceManager 失败
- Carla notes (04) - client and world (create client, connect world, batch object, set weather, set lights, world snapshots)
- Schematic diagram of C measuring tool
- @Repository详解
- RTL8762DK 环境搭建(一)
猜你喜欢

递归方法实现最大公约数

Group division and characteristic analysis of depression patients based on online consultation records

SLAM综述阅读笔记七:Visual and Visual-Inertial SLAM: State of the Art, Classification,and Experimental 2021

Navicate报错access violation at address 00000000

基于招聘广告的岗位人才需求分析框架构建与实证研究

NFT 的 10 种实际用途

Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading

JS what is declaration in advance? The order of function and variable declaration in advance (the foreshadowing of execution context)

DVWA full level customs clearance tutorial

JS epidemic at home, learning can't stop, 7000 word long text to help you thoroughly understand the prototype and prototype chain
随机推荐
windows10 安装Sql Server 2019
Getting started for beginners: build your own blog with WordPress
Failed to connect to ResourceManager
软件产品第三方测试费用为什么没有统一的报价?
@Repository详解
Hdu1422 revisits the world cup [DP]
JS什么是声明提前?函数与变量声明提前的先后顺序(执行上下文铺垫篇)
Hdu4496 d-city [concurrent search]
Weice biological IPO meeting: annual revenue of 1.26 billion Ruihong investment and Yaohe medicine are shareholders
MySQL advanced II. Logical architecture analysis
2022年中国网络视频市场年度综合分析
Toward fast, flexible, and robust low light image enhancement cvpr2022
初学者入门:使用WordPress搭建一个专属自己的博客
JS 疫情宅在家,学习不能停,七千字长文助你彻底弄懂原型与原型链
uniapp的request数据请求简单封装步骤
Shell编程规范与变量
2022 Niuke multi School II_ E I
「游戏引擎 浅入浅出」4.1 Unity Shader和OpenGL Shader
Mining enterprise association based on Enterprise Knowledge Map
基于在线问诊记录的抑郁症病患群组划分与特征分析