[AcWing算法基础课] 五.动态规划

包括背包问题,线性DP,区间DP,计数类DP,数位统计DP,状态压缩DP,树形DP,记忆化搜索等内容。


目录

背包问题

01背包

完全背包

多重背包

分组背包

线性DP

状态压缩DP



背包问题

01背包

2. 01背包问题 - AcWing题库

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1010;
int v[N],w[N],f[N];

int main()
{
    int N,V;
    cin >> N >> V;
    for (int i = 1; i <= N; i ++ ) cin >> v[i] >> w[i];
    for (int i = 1; i <= N; i ++ )
        for (int j = V; j >= v[i]; j -- )
            f[j]=max(f[j],f[j-v[i]]+w[i]);
    cout << f[V];
    return 0;
}

完全背包

3. 完全背包问题 - AcWing题库

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1010;
int v[N],w[N],f[N];

int main()
{
    int N,V;
    cin >> N >> V;
    for (int i = 1; i <= N; i ++ ) cin >> v[i] >> w[i];
    for (int i = 1; i <= N; i ++ )
        for (int j = v[i]; j <= V; j ++ )
            f[j]=max(f[j],f[j-v[i]]+w[i]);
    cout << f[V];
    return 0;
}

多重背包

多重背包问题的二维优化_☆迷茫狗子的秘密基地☆-CSDN博客5. 多重背包问题 II - AcWing题库输入样例4 51 2 32 4 13 4 34 5 2输出样例:10朴素做法:#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int M = 110;int v[M],w[M],s[M],f[M][M];int N,V;int main(){...https://blog.csdn.net/qq_39391544/article/details/120432990

分组背包

9. 分组背包问题 - AcWing题库高质量的算法题库https://www.acwing.com/problem/content/9/

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 110;
int v[N][N],w[N][N],s[N],f[N];
int main()
{
    int n,V;
    cin >> n >> V;
    for (int i = 1; i <= n; i ++ ){
        cin >> s[i];
        for (int j = 1; j <= s[i]; j ++ ){
            cin >> v[i][j] >> w[i][j];
        }
    }
    
    for (int i = 1; i <= n; i ++ )
        for (int j = V; j >= 0; j -- )
            for (int k = 1; k <= s[i]; k ++ )
                if(v[i][k]<=j)
                    f[j] = max(f[j],f[j-v[i][k]]+w[i][k]);
                    
    cout << f[V];
    return 0;
}

线性DP

[简单DP]LP钱不够(数塔问题)_☆迷茫狗子的秘密基地☆-CSDN博客链接:https://ac.nowcoder.com/acm/problem/14582来源:牛客网★★★LP钱不够吃货LP参加了珠海美食节,每见一家摊位都会大吃一顿,但是如果不加收敛,接下来的日子就只能吃土了,所以,他决定只向前,不回头,花最少的钱,在美食节上吃出一条血路。在美食节的矩形地图中,LP站在左上角的入口,请帮助Ta到达右下角的出口。输入描述:第一行包含一个正整数T(T<=10),表示有T组测试数据。每组数据第一行包含一个正整数n(3 <= n<=20.https://blog.csdn.net/qq_39391544/article/details/118381231

最长上升子序列的溯源和二分优化_☆迷茫狗子的秘密基地☆-CSDN博客目录(数据范围为1~1000时)最长上升子序列的溯源(数据范围为1~100000时)(数据范围为1~1000时)输入样例:73 1 2 1 8 5 6输出样例:4#include <iostream>#include <algorithm>using namespace std;const int N = 1010;int a[N],f[N];int main(){ int n; cin >...https://blog.csdn.net/qq_39391544/article/details/120534870[初学区间DP时的意识流] AcWing 282. 石子合并_☆迷茫狗子的秘密基地☆-CSDN博客例题:282. 石子合并 - AcWing题库输入样例:41 3 5 2输出样例:22思路简述:如何求每一段连续区间的最小代价呢?①将其分为两部分,举个栗子,这个区间左右边界下标分别为 3 , 10, 那么我们依次将其分为两部分(每部分至少有一个元素),如下九种情况3 | 4~103~4 | 5~103~5 | 6~10……3~9 | 10②那么我们如何确立左右边界下标呢? 首先我们需要明确我们要处理...https://blog.csdn.net/qq_39391544/article/details/120554841

状态压缩DP

[★状态压缩DP★] AcWing 291. 蒙德里安的梦想_☆迷茫狗子的秘密基地☆-CSDN博客输入样例:1 21 31 42 22 32 42 114 110 0输出样例:10123514451205#include<iostream>#include<cstring>#include<vector>#include<algorithm>using namespace std;typedef long long LL;const int N = 12, M = 1<...https://blog.csdn.net/qq_39391544/article/details/120608203?spm=1001.2014.3001.5501

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泥烟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值