DP问题杂烩

本文介绍了动态规划中的线性DP、背包DP和树形DP的应用,包括最长上升子序列、背包问题和最大连通子块的解决方法。通过实例解析和代码展示,阐述了不同类型的DP问题的解决思路和优化技巧,如单调队列优化和滚动数组的使用。
摘要由CSDN通过智能技术生成

目录:

一. 线性DP
二. 背包DP
三. 树形DP

一.线性DP

1.最长上升子序列问题:

导弹拦截:

题目链接:https://www.luogu.com.cn/problem/P1020

dp做法:复杂度O(n ^ 2) 显然过不了

状态表示:f[i] 表示前i个数字中,最长上升子序列的长度(思路很简单,不赘述)

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1e6 + 10;

int f[N], a[N];

int main()
{
    int n = 0;
    while(cin >> a[n ++]);
    for(int i = 0; i < n - 1; i ++ ) {
        f[i] = 1;
        for(int j = 0; j < i; j ++ )
            if(a[i] > a[j])
                f[i] = f[j] + 1;
    }
    
    int len = 0;
    for(int i = 0; i < n - 1; i ++ ) 
        len = max(f[i], len);
        
    cout << len; 
    
        
}

单调队列做法(复杂度O(n * logn)):

题目分析:第一问很容易理解,求一个最长不上升子序列;

第二问:对于任意一个系统来说,每次多加一个系统,都是因为有一个上升的数字,它不能维护。

也就是说求一个最长上升子序列。也可以这么理解,对于任意两个系统的划分依据是:前一个系统的某一个的高度小于后一个系统的某一高度。

处理:用一个q来维护单调队列,每次二分查找大于等于a[i]的最小的数,然后在它的后面填上a[i](以最长不上升子序列为例)。

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1e6 + 10;

int q[N], n, a[N];
int p[N];

int main()
{
    while(cin >> a[n ++]);
    
    int len = 0, cnt = 0;
    
    n --;
    
    for(int i = 0; i < n; i ++ ) {
        int l = 0, r = len;
        
        while(l < r) {
            int mid = l + r + 1 >> 1;
            if(q[mid] >= a[i]) l = mid;//找出第一个大于等于它的数字
            else r = mid - 1;
        }
        
        len = max(len, r + 1);
        q[r + 1] = a[i];
    }
    
    
    for(int i = 0; i < n; i ++ ) {
        int l = 0, r = cnt;
        while(l < r) {
            int mid = l + r + 1 >> 1;
            if(p[mid] < a[i]) l = mid;
            else r = mid - 1;
        }
        
        cnt = max(cnt, r + 1);
        p[r + 1] = a[i];
        
    }
    
    cout << len << " " << cnt;
}

二.背包dp问题

1.背包问题的抽象化处理

垃圾陷阱:

题目链接:https://www.luogu.com.cn/problem/P1156

状态表示:f[i][j], 选择前i个垃圾,达到j高度所能维持的最大存活时间。

状态转移方程:(当前存活才可以等到垃圾)f[i][j + h] = f[i - 1][j]; f[i][j] = f[i - 1][j] + life;

注意:1.如果等不到当前垃圾出现吃掉它,也要把当前的i由i - 1转移过来, 否则后面都会为0,转移不过去。

2.用滚动数组优化不需要考虑注意1,但是一定要记住转移顺序(更新顺序)不能反了。(debug太久了)

二维数组:

#include <bits/stdc++.h>

using namespace std;

int f[5000][110];
int d, g;

struct Node{
    int t, l, h;
    bool operator < (const Node x) const {
        return t < x.t;
    }
}node[110];

int main()
{
    cin >> d >> g;
    
    for(int i = 1; i <= g; i ++ ) {
        int a, b, c;
        cin >> a >> b >> c;
        node[i] = {a, b, c};
    }
    
    sort(node + 1, node + 1 + g);
    
    f[0][0] = 10;
    for(int i = 1; i <= g; i ++ ) {
        f[i][0] = f[i - 1][0];
        for(int j = 0; j <= d; j ++ ) {
            if(f[i - 1][j] >= node[i].t) {
                if(j + node[i].h >= d) {
                    cout << node[i].t << endl;
                    return 0;
                }
                //吃
                f[i][j] = max(f[i][j], f[i - 1][j] + node[i].l);
                //不吃
                f[i][j + node[i].h] = max(f[i][j + node[i].h], f[i - 1][j]); 
                
            }
        }
    }
        
    cout << f[g][0];
    return 0;
}

滚动数组(优化为一维,注意更新顺序!):

#include <bits/stdc++.h>

using namespace std;

int f[110];
int d, g;

struct Node{
    int t, l, h;
    bool operator < (const Node x) const {
        return t < x.t;
    }
}node[110];

int main()
{
    cin >> d >> g;
    
    int sum = 0;
    for(int i = 1; i <= g; i ++ ) {
        int a, b, c;
        cin >> a >> b >> c;
        node[i] = {a, b, c};
    }
    
    sort(node + 1, node + 1 + g);
    
    f[0] = 10;
    for(int i = 1; i <= g; i ++ )
        for(int j = d; j >= 0; j -- ) {
            if(f[j] >= node[i].t) {
                if(j + node[i].h >= d) {
                    cout << node[i].t << endl;
                    return 0;
                }
                //不吃
                f[j + node[i].h] = max(f[j + node[i].h], f[j]); 
                //吃
                f[j] = f[j] + node[i].l;
            }
        }
    cout << f[0];
    return 0;
}

三. 树形dp问题:

1.最大连通子块问题:

筑巢:

题目链接:https://ac.nowcoder.com/acm/contest/11222/E

状态表示 :f[u]表示当前子连通块的最大值

细节:不要用已经更新好的f[u]重复去更新父节点

邻接表(链式前向星)写法:

#include <bits/stdc++.h>
//树形dp问题
using namespace std;

typedef long long LL;

const int N = 2e5 + 10;
const int INF = 0x3f3f3f3f;

LL f[N];
int val[N];
int h[N], ne[N], e[N] ,idx, w[N];
bool st[N];
LL ans = -INF;

void add(int a, int b, int c){
    e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}

void dfs(int u){
    if(st[u]) return ;
    
    st[u] = true;
    f[u] = val[u];
    
    for(int i = h[u]; i != -1; i = ne[i] ) {
        int j = e[i];
        if(st[j]) continue;
        dfs(j);
        
        f[u] = max(f[u], f[u] + w[i] + f[j]);
    }
}

int main()
{
    memset(h, -1, sizeof h);
    int n;
    cin >> n;
    
    for(int i = 1; i <= n; i ++ ) 
        cin >> val[i];
    
    for(int i = 1; i < n; i ++ ) {
        int a, b, c;
        cin >> a >> b >> c;
        
        add(a, b, c);
        add(b, a, c);
    }
    

    for(int i = 1; i <= n; i ++ ) {
        if(!st[i])
        dfs(i);
    }
    
    for(int i = 1; i <= n; i ++ )
        ans = max(ans, f[i]);
    
    cout << ans;
    
    return 0;
    
}

vector写法:

//树上最大的连通子块
#include <bits/stdc++.h>
#define int long long

using namespace std;
typedef pair<int, int> PII;

const int N = 1e5 + 10;

//用vectorx写一下
vector<PII> edge[N];
int val[N], n, f[N];//f为dp维护当前子树连通块的最大值
bool st[N];//当前这个点有没有被搜过

void dfs(int u) {
    if(st[u]) return ;
    
    st[u] = true;
    f[u] = val[u];
    for(PII t : edge[u]) {
        int j = t.first, w = t.second;
        //如果说这个点没有被更新过,才可以去dfs这个点,并且用这个子块去更新母子快 
        if(st[j]) continue;
            dfs(j);
        //如果用已经更新过的点更新的话,会把已经更新好的节点重复累加
        f[u] = max(f[u], f[u] + f[j] + w);
    }
}

signed main()
{
    cin >> n;
    for(int i = 1; i <= n; i ++ ) cin >> val[i];
    
    for(int i = 0; i < n - 1; i ++ ) {
        int a, b, c;
        cin >> a >> b >> c;
        edge[a].push_back({b, c});
        edge[b].push_back({a, c});
    }
    
    for(int i = 1; i <= n; i ++ ) {
        if(!st[i])
            dfs(i);
    }
    int ans = -2e9;
    
    for(int i = 1; i <= n; i ++ ) {
        ans = max(ans, f[i]);
    }
    
    cout << ans;
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值