当前位置:网站首页>股票买卖4
股票买卖4
2022-07-27 13:29:00 【战士小小白】
股票买卖 IV
给定一个长度为 N 的数组,数组中的第 i 个数字表示一个给定股票在第 i 天的价格。
设计一个算法来计算你所能获取的最大利润,你最多可以完成 k 笔交易。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。一次买入卖出合为一笔交易。
输入格式
第一行包含整数 N 和 k,表示数组的长度以及你可以完成的最大交易笔数。
第二行包含 N 个不超过 10000 的正整数,表示完整的数组。
输出格式
输出一个整数,表示最大利润。
数据范围
1≤N≤1051≤N≤105,
1≤k≤1001≤k≤100
输入样例1:
3 2
2 4 1
输出样例1:
2
输入样例2:
6 2
3 2 6 5 0 3
输出样例2:
7
样例解释
样例1:在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。
样例2:在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。共计利润 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;
}

边栏推荐
- Research on automatic classification of electronic medical records based on unbalanced data
- @Bean 与 @Component 用在同一个类上,会发生什么?
- Vscode -- create template file
- Thread knowledge summary
- MySQL advanced II. Logical architecture analysis
- Simple encapsulation steps of request data request of uniapp
- HDU4496 D-City【并查集】
- 2022牛客多校二_ E I
- Docker实践经验:Docker 上部署 mysql8 主从复制
- JS什么是声明提前?函数与变量声明提前的先后顺序(执行上下文铺垫篇)
猜你喜欢

Windows10 installing SQL Server 2019

Research on automatic classification of electronic medical records based on unbalanced data

this指向问题,闭包以及递归

Simple encapsulation steps of request data request of uniapp

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

Dako held a meeting for the biological IPO: the annual revenue was 837million, and Wu Qingjun and his daughter were the actual controllers

【论文精读】Grounded Language-Image Pre-training(GLIP)

Pure C handwriting thread pool

Docker实践经验:Docker 上部署 mysql8 主从复制

Excellent basic methods of URL parsing using C language
随机推荐
C language layered understanding (C language array)
Win11壁纸变黑怎么办?Win11壁纸变黑了的解决方法
Docker实践经验:Docker 上部署 mysql8 主从复制
Database storage series (1) column storage
SLAM综述阅读笔记四:A Survey on Deep Learning for Localization and Mapping: Towards the Age of Spatial 2020
万字详解 Google Play 上架应用标准包格式 AAB
Why does script file 'd:\anaconda3\envs\pad appear_ env\Scripts\pip-script. py‘ is not present.
【医疗行业】DICOM converter Tools
va_ List usage summary
Annual comprehensive analysis of China's online video market in 2022
Advanced MySQL III. storage engine
Named entity recognition of Chinese electronic medical records based on Roberta WwM dynamic fusion model
[related contents of multithreading]
基于预训练模型的多标签专利分类研究
How to view revenue and expenditure by bookkeeping software
基于GEC6818开发板的相册
Research on multi label patent classification based on pre training model
次小生成树【模板】
Import the virtual machine officially made by Kali Linux into Oracle VirtualBox
Slam overview Reading Note 6: slam research based on image semantics: application-oriented solutions for autonomous navigation of mobile robots 2020