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

边栏推荐
- 10 practical uses of NFT
- 解气!哈工大被禁用MATLAB后,国产工业软件霸气回击
- va_list 使用总结
- What you want most is the most comprehensive summary of C language knowledge. Don't hurry to learn
- Architecture - the sublimation of MVC
- Chapter 3 business function development (add clues and remarks, and automatically refresh the added content)
- 文献翻译__tvreg v2:用于去噪、反卷积、修复和分割的变分成像方法(部分)
- Interview eight part essay · TCP protocol
- telnet远程登录aaa模式详解【华为eNSP】
- 炒黄金开户平台有没有正规,安全的
猜你喜欢

知识关联视角下金融证券知识图谱构建与相关股票发现

进程间通信

Docker practical experience: deploy mysql8 master-slave replication on docker

SLAM综述阅读笔记四:A Survey on Deep Learning for Localization and Mapping: Towards the Age of Spatial 2020

C language layered understanding (C language array)

Thread knowledge summary

Toward fast, flexible, and robust low light image enhancement cvpr2022

@Bean 与 @Component 用在同一个类上,会发生什么?

Chapter 3 business function development (add clues and remarks, and automatically refresh the added content)

Simple encapsulation steps of request data request of uniapp
随机推荐
Ten thousand words detailed Google play online application standard package format AAB
线程知识总结
Secondary spanning tree [template]
Cultural tourism and data collection | travel to Yunnan in an artistic way
Docker实践经验:Docker 上部署 mysql8 主从复制
【论文精读】Grounded Language-Image Pre-training(GLIP)
Why does script file 'd:\anaconda3\envs\pad appear_ env\Scripts\pip-script. py‘ is not present.
SLAM综述阅读笔记六:基于图像语义的SLAM调研:移动机器人自主导航面向应用的解决方案 2020
@What happens when bean and @component are used on the same class?
Graphic SQL of giant image
正则表达式:邮箱匹配
Redis
Flexible and easy to use WYSIWYG visual report
Photo album based on gec6818 development board
Is the security of online brokerage app account opening guaranteed?
JS epidemic at home, learning can't stop, 7000 word long text to help you thoroughly understand the prototype and prototype chain
FPGA时序约束分享04_output delay 约束
知识关联视角下金融证券知识图谱构建与相关股票发现
Unity3D学习笔记10——纹理数组
终于有人把面试必考的动态规划、链表、二叉树、字符串全部撸完了