每日算法 – day 24

每日算法

those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out


那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

2020.3.9


luogu -P1049 装箱问题

假设数据 tank = 5
n = 3
v : 3 4 2

nv 1 2 3 4 5
1 0 -1 -1 -1 5
2 -1 2 -1 -1 4
3 0 2 -1 -1 2
4 0 0 0 -1 0

从搜索的角度去考虑动态规划问题

  1. 每个物品只有选和不选两种可能
  2. 不合理的分支将会被减去 故上表出现很多 -1
  3. 存在已经算过的问题比如 dfs(2 , 5) 会用到 dfs(4 , 3) ,而dfs(3,5) 也会用到 dfs(4 , 3) 我们可以将第一次进入dfs(4 , 3)的时候将该状态记录下来,从而达到优化搜索的目的。

这就是动态规划问题的一种思考方式 记忆化搜索
每日算法 - day 24

记忆化搜索版本

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

const int N = 20005;
const int M = 35;

int f[M][N] , n , v[N] , tank;

int dfs(int now,int space)
{
    if(f[now][space] != -1)return f[now][space];
    if(now == n + 1)
    {
        return f[now][space] = 0;
    }
    int f1 = -999,f2 = -999;
    f1 = dfs(now + 1,space); // 表示当前物品不选
    if(space >= v[now]) // 表示可以选 
    {
        f2 = dfs(now + 1,space - v[now]) + v[now]; 
    }
    return f[now][space] = max(f1,f2); 
}
int main()
{

    fill(f[0],f[0] + M * N , -1);   
    cin >>    tank >> n;
    for(int i = 1;i <= n ;i ++)
    {
        cin >> v[i];
    }

    cout << tank - dfs(1,tank);
    return 0;
} 

递推版本
递推式动态规划的另外一种主要形式

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;
const int N = 20005;
const int M = 35;
int f[M][N] , n , v[M] , tank;

int main()
{
    cin >> tank >> n;
    for(int i = 1;i <= n ;i ++)cin >> v[i];

    for(int i = 1;i <= n ;i ++)
    {
        for(int j = 1;j <= tank;j ++)
        {
            if(j < v[i])
            {
                f[i][j] = f[i-1][j];
            }else{
                f[i][j] = max(f[i-1][j],f[i-1][j-v[i]] + v[i]);
            }
        }   
    }   
    cout << tank -  f[n][tank];
    return 0;
}

luogu -P1216 数字三角形

dp动规

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;
const int N = 1010;

int a[N][N] ,r , f[N][N];

int main()
{
    cin >> r;

    for(int i = 1;i <= r;i ++)
    {
        for(int j = 1;j <= i ;j ++)
        {
            scanf("%d",&a[i][j]);
        }
    } 
    int ans = 0;
    for(int i = 1;i <= r ;i ++)
    {
        for(int j = 1;j <= i ;j ++)
        {
            f[i][j] += max(f[i-1][j-1],f[i-1][j]) + a[i][j];
            ans = max(ans,f[i][j]);
        }
    }
    cout << ans << endl;
    return 0;
}

记忆化搜索版

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;
const int N = 1010;

int a[N][N], f[N][N] , r;

int dfs(int i ,int j)
{
    if(f[i][j] != -1)return f[i][j];

    if(i == r + 1)
    {
        return 0;
    }
    int l = dfs(i + 1, j);
    int r = dfs(i + 1, j + 1);
    return f[i][j] = max(l,r) + a[i][j];
}
int main()
{

    fill(f[0],f[0] + N * N , -1);
    cin >> r;
    for(int i = 1;i <= r;i ++)
    {
        for(int j = 1; j <= i; j++)
        {
            scanf("%d",&a[i][j]);   
        }   
    }           
    cout << dfs(1,1) << endl;
    return 0;
} 

原文链接: https://www.cnblogs.com/wlw-x/p/12452405.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    每日算法 - day 24

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/334358

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午9:38
下一篇 2023年3月1日 下午9:38

相关推荐