当前位置:网站首页>状态机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;
}
边栏推荐
猜你喜欢
Thinkphp6 realizes database backup
【暑期每日一题】洛谷 P6461 [COCI2006-2007#5] TRIK
Comparison of advantages between can & canfd integrated test analysis software lkmaster and PCA Explorer 6 analysis software
Practice of online problem feedback module (XVII): realize the online download function of excel template
NFT 的 10 种实际用途
Some learning and understanding of vintage analysis
Amazon cloud assistant applet is coming!
【OpenGL】着色器(Shader)的使用
It's enough for MySQL to have this article (disgusting and crazy typing 37k words, just for Bo Jun's praise!!!)
Using C language to skillfully realize the chess game -- Sanzi chess
随机推荐
7-2 计算正五边形的面积和周长 (25分)
【暑期每日一题】洛谷 P6500 [COCI2010-2011#3] ZBROJ
2022-07-28: what is the output of the following go language code? A:AA; B:AB; C:BA; D:BB。 package main import ( “fmt“ ) func main() { f
It's enough for MySQL to have this article (disgusting and crazy typing 37k words, just for Bo Jun's praise!!!)
3-global exception handling
mysql 单表最多能存多少数据?
WPF interface layout must know basis
Scala higher order (10): exception handling in Scala
Leetcode 209. subarray with the smallest length (2022.07.28)
Remote invocation of microservices
在线问题反馈模块实战(十七):实现excel模板在线下载功能
H3C_ Using setting default static routing priority to realize the active and standby function of export dual lines
WPF nested layout case
Leetcode buckle classic problem -- 4. Find the median of two positively ordered arrays
leetcode力扣经典问题——4.寻找两个正序数组的中位数
【暑期每日一题】洛谷 P1601 A+B Problem(高精)
jdbc入门
分析25个主要DeFi协议的路线图 预见DeFi未来的七大趋势
MySQL 使用客户端以及SELECT 方式查看 BLOB 类型字段内容总结
我,28岁,测试员,10月无情被辞:想给还在学测试 的人提个醒......