当前位置:网站首页>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;
}

边栏推荐
- Document translation__ Tvreg V2: variational imaging method for denoising, deconvolution, repair and segmentation (part)
- Advanced MySQL III. storage engine
- 架构——MVC的升华
- 【STM32】EXTI
- PROFINET 模拟器使用教程
- Photo album based on gec6818 development board
- C语言基础知识梳理总结
- Getting started for beginners: build your own blog with WordPress
- Database storage series (1) column storage
- 【STM32】EXTI
猜你喜欢

Ten thousand words detailed Google play online application standard package format AAB

10 practical uses of NFT

User question understanding and answer content organization for epidemic disease Science Popularization

Import the virtual machine officially made by Kali Linux into Oracle VirtualBox

Advanced MySQL III. storage engine

终于有人把面试必考的动态规划、链表、二叉树、字符串全部撸完了

Unity3d learning note 10 - texture array

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

codeforces 1708E - DFS Trees

DVWA full level customs clearance tutorial
随机推荐
Shell programming specifications and variables
网上券商APP开户安全有保障吗?
架构——MVC的升华
线程知识总结
@Repository详解
Chapter 3 business function development (view clue details)
User question understanding and answer content organization for epidemic disease Science Popularization
Kubernetes 节点磁盘故障排查
Simple encapsulation steps of request data request of uniapp
软件产品第三方测试费用为什么没有统一的报价?
Forward proxy and reverse proxy
Positive mask, negative mask, wildcard
基于招聘广告的岗位人才需求分析框架构建与实证研究
知识关联视角下金融证券知识图谱构建与相关股票发现
Advanced MySQL III. storage engine
终于有人把面试必考的动态规划、链表、二叉树、字符串全部撸完了
Detailed explanation of Telnet remote login AAA mode [Huawei ENSP]
Hdu1422 revisits the world cup [DP]
Carla notes (04) - client and world (create client, connect world, batch object, set weather, set lights, world snapshots)
HDU4565 So Easy! [matrix multiplication] [derivation]