当前位置:网站首页>Stock trading 4
Stock trading 4
2022-07-27 14:43:00 【Soldier Xiaobai】
Stock trading IV
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 you can get , You can do at most k transaction .
Be careful : You can't participate in multiple transactions at the same time ( You have to sell the stock before you buy it again ). One buy and one sell is one transaction .
Input format
The first line contains integers N and k, Indicates the length of the array and the maximum number of transactions you can complete .
The second line contains N No more than one. 10000 The positive integer , Represents a complete array .
Output format
Output an integer , It means maximum profit .
Data range
1≤N≤1051≤N≤105,
1≤k≤1001≤k≤100
sample input 1:
3 2
2 4 1
sample output 1:
2
sample input 2:
6 2
3 2 6 5 0 3
sample output 2:
7
Sample explanation
Examples 1: In the 1 God ( Stock price = 2) Buy when , In the 2 God ( Stock price = 4) Sell when , The exchange will make a profit = 4-2 = 2 .
Examples 2: In the 2 God ( Stock price = 2) Buy when , In the 3 God ( Stock price = 6) Sell when , The exchange will make a profit = 6-2 = 4 . And then , In the 5 God ( Stock price = 0) Buy when , In the 6 God ( Stock price = 3) Sell when , The exchange will make a profit = 3-0 = 3 . Total profit 4+3 = 7.
#include <bits/stdc++.h>
using namespace std;
const int N = 100010, M = 110;
int n, m;
int w[N];
int f[N][M][2];
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++) cin >> w[i];
memset(f, -0x3f, sizeof f);
for(int i = 0; i <= n; i ++) f[i][0][0] = 0;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
{
f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j][1] + w[i]);
f[i][j][1] = max(f[i - 1][j][1], f[i - 1][j - 1][0] - w[i]);
}
int res = 0;
for(int i = 1; i <= m; i ++) res = max(res, f[n][i][0]);
printf("%d\n", res);
return 0;
}

边栏推荐
- Detoxify! After Harbin Institute of technology was disabled MATLAB, domestic industrial software fought back domineering
- Construction and empirical research of post talent demand analysis framework based on recruitment advertisement
- @Repository详解
- [intensive reading of papers] grounded language image pre training (glip)
- 次小生成树【模板】
- FPGA timing constraint sharing 04_ Output delay constraint
- Toward Fast, Flexible, and Robust Low-Light Image Enhancement(实现快速、灵活和稳健的弱光图像增强)CVPR2022
- watch VS watchEffect
- 【STM32】EXTI
- Mining enterprise association based on Enterprise Knowledge Map
猜你喜欢

Unity3D学习笔记10——纹理数组

Document translation__ Tvreg V2: variational imaging method for denoising, deconvolution, repair and segmentation (part)

Interview eight part essay · TCP protocol

Leetcode · daily question · 592. fraction addition and subtraction · simulation

CPU、GPU、NPU的区别

Research on Chinese idiom metaphorical knowledge recognition and relevance based on transfer learning and text enhancement

Arduino+ze08-ch2o formaldehyde module, output formaldehyde content

What if win11 wallpaper turns black? The solution of win11 wallpaper blackening

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

Recursive method to realize the greatest common divisor
随机推荐
Understand JS execution context in an article
[cache series] completely solve the problems of cache avalanche, breakdown and penetration
Is there a regular and safe account opening platform for gold speculation
Group division and characteristic analysis of depression patients based on online consultation records
How to return to the parent directory with commands
文献翻译__tvreg v2:用于去噪、反卷积、修复和分割的变分成像方法(部分)
Getting started for beginners: build your own blog with WordPress
Toward fast, flexible, and robust low light image enhancement cvpr2022
【STM32】EXTI
Kubernetes 节点磁盘故障排查
[note] logistic regression
Slam overview Reading Note 6: slam research based on image semantics: application-oriented solutions for autonomous navigation of mobile robots 2020
This points to problems, closures, and recursion
万字详解 Google Play 上架应用标准包格式 AAB
PROFINET 模拟器使用教程
[related contents of multithreading]
Photo album based on gec6818 development board
【STM32】EXTI
Hdu1422 revisits the world cup [DP]
Why does script file 'd:\anaconda3\envs\pad appear_ env\Scripts\pip-script. py‘ is not present.