当前位置:网站首页>状态机dp三维
状态机dp三维
2022-07-29 07:15:00 【一条小小yu】
给定一个长度为 NN 的数组,数组中的第 ii 个数字表示一个给定股票在第 ii 天的价格。
设计一个算法来计算你所能获取的最大利润,你最多可以完成 kk 笔交易。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。一次买入卖出合为一笔交易。
输入格式
第一行包含整数 NN 和 kk,表示数组的长度以及你可以完成的最大交易笔数。
第二行包含 NN 个不超过 1000010000 的正整数,表示完整的数组。
输出格式
输出一个整数,表示最大利润。
数据范围
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;
typedef long long ll;
typedef double db;
const int N=1e5+5,M=110,INF=0x3f3f3f3f;
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=0;i<=m;i++)res=max(res,f[n][i][0]);
cout<<res;
return 0;
}
边栏推荐
- 3-全局异常处理
- Scala higher order (IX): pattern matching in Scala
- Does Flink support sqlserver databases? Get the changes of SQLSERVER database
- 【暑期每日一题】洛谷 P4414 [COCI2006-2007#2] ABC
- Docker最新超详细教程——Docker创建运行MySQL并挂载
- WPF simple login page completion case
- Gin template
- Vagrant box cluster processing
- QT专题:基础部件(按钮类,布局类,输出类,输入类,容器类)
- 能在SQL 语句中 指定 内存参数吗?
猜你喜欢

How to establish EDI connection with Scania in Scania?

jdbc入门

WPF interface layout must know basis

PAT甲级 1146 拓扑顺序

Latest 10 billion quantitative private placement list

leetcode力扣经典问题——4.寻找两个正序数组的中位数

halcon的安装以及在vs2017中测试,vs2017中dll的配置

Thoroughly understand kubernetes scheduling framework and plug-ins

QT topic: basic components (button class, layout class, output class, input class, container class)

Using C language to skillfully realize the chess game -- Sanzi chess
随机推荐
A long article --- in-depth understanding of synchronized
logback 中FileAppender具有什么功能呢?
Latest 10 billion quantitative private placement list
Nodejs installation tutorial
Female graduate students do "mind mapping" and quarrel with their boyfriend! Netizen: the "king of infighting" in the quarrel
Description of rollingfileappender attribute in logback
用户列表 圆形头像并跟随小板块
Custom events
Amazon cloud assistant applet is coming!
Spingboot integrates the quartz framework to realize dynamic scheduled tasks (support real-time addition, deletion, modification and query tasks)
js中break与continue和return关键字
Can I specify memory parameters in SQL statements?
Tp6 use protobuf
对Vintage分析的一些学习理解
美智光电IPO被终止:年营收9.26亿 何享健为实控人
新生代公链再攻「不可能三角」
【MYSQL】-【子查询】
5-integrate swagger2
Interface test actual project 03: execute test cases
do end用法的妙处