当前位置:网站首页>Dynamic programming - stock trading 5
Dynamic programming - stock trading 5
2022-07-27 14:43:00 【Soldier Xiaobai】
Stock trading V
Given a length of N Array of , The... In the array i A number indicates that a given stock is on the i Sky price .
Design an algorithm to calculate the maximum profit . Under the following constraints , You can do as many deals as you can ( Buy and sell a stock many times ):
- You can't participate in multiple transactions at the same time ( You have to sell the stock before you buy it again ).
- After sale of shares , You can't buy shares the next day ( That is, the freezing period is 11 God ).
Input format
The first line contains integers N, Represents the array length .
The second line contains N No more than one. 1000010000 The positive integer , Represents a complete array .
Output format
Output an integer , It means maximum profit .
Data range
1≤N≤1051≤N≤105
sample input :
5
1 2 3 0 2
sample output :
3
Sample explanation
The corresponding transaction status is : [ purchase , sell , Freezing period , purchase , sell ], Profit from the first transaction 2-1 = 1, Profit from the second transaction 2-0 = 2, Shared profits 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;
}

边栏推荐
- 动态规划——股票买卖5
- JS what is declaration in advance? The order of function and variable declaration in advance (the foreshadowing of execution context)
- STM32 - capacitive touch button experiment
- 巨形象的图解 SQL
- 开源版思源怎么私有部署
- DVWA全级别通关教程
- 融合迁移学习与文本增强的中文成语隐喻知识识别与关联研究
- Secondary spanning tree [template]
- Cultural tourism and data collection | travel to Yunnan in an artistic way
- Architecture - the sublimation of MVC
猜你喜欢

JS 疫情宅在家,学习不能停,七千字长文助你彻底弄懂原型与原型链

DVWA全级别通关教程

What you want most is the most comprehensive summary of C language knowledge. Don't hurry to learn

10 practical uses of NFT
![Detailed explanation of Telnet remote login AAA mode [Huawei ENSP]](/img/48/dd0fa3c494c45f604b7a93c707b808.png)
Detailed explanation of Telnet remote login AAA mode [Huawei ENSP]

Chinese character style transfer --- antagonistic discriminative domain adaptation (L1)

Construction and empirical research of post talent demand analysis framework based on recruitment advertisement

JS epidemic at home, learning can't stop, 7000 word long text to help you thoroughly understand the prototype and prototype chain

在Oracle VirtualBox中导入Kali Linux官方制作的虚拟机

线程知识总结
随机推荐
Research on automatic classification of electronic medical records based on unbalanced data
软件产品第三方测试费用为什么没有统一的报价?
[note] logistic regression
连接ResourceManager 失败
@Repository详解
DXGI 方式采集流程
log4j2 jdbc appender
Database storage series (1) column storage
PROFINET 模拟器使用教程
Printf function buffer problem
Who can't capture packets these days? Wireshark packet capture and common protocol analysis are for you!
Lesson 3: reverse word order
What if win11 wallpaper turns black? The solution of win11 wallpaper blackening
STM32——电容触摸按键实验
Simple encapsulation steps of request data request of uniapp
次小生成树【模板】
UnityUI方面处理(归纳与积累)
自动化配置SSH免密登录和取消SSH免密配置脚本
Summary of basic knowledge of C language
Unity3D学习笔记10——纹理数组