每日算法 - day 26

每日算法

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.11


luogu -P1044 栈

dp 做法 之前卡特兰数感觉挺懵得 也没学到 还是等之后学数论得时候专门看卡特兰吧

关键在于抽象化操作,每个栈中得元素只有出队和入队两种可能,和我们做大部分题目时有点类似之处,只有选和不选,走和不走啊之类得
f[i][j] 表示 当前队列中有 i 个 栈里有 j 个

f[i][j] = f[i-1][j] + f[i][j-1]
即当栈中有 i 个元素时 ,
要么将当前位置弹出,要么从队列中取出来一个

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

using namespace std;
typedef long long ll;

const int N = 20;

ll f[N][N] , n;
int main()
{
    
    cin >> n;
    for(int i = 0;i <= n ;i ++)
        f[i][0] = 1;  //队列中方没有候选数字只能进栈 
    for(int j = 1;j <= n ;j ++)
    {
        for(int i = 0;i <= n ;i ++)
        {
            if(i >= 1)
                f[i][j] = f[i-1][j] + f[i+1][j-1];
            if(i == 0) //栈内为空
                f[i][j] = f[i+1][j-1];
        }   
    }   
    cout << f[0][n];
    return 0;   
} 

luogu -P1164 小A点菜

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

using namespace std;

const int M = 100010;
const int N = 1010;

int f[N][M] , n , m;
int v[N]; 
int main()
{
    
    cin >> n >> m;
    for(int i = 1;i <= n ;i ++)
    {
        scanf("%d",&v[i]); 
    }
    
    for(int i = 1;i <= n ;i ++)
    {
        for(int j = 1;j <= m ;j ++)
        {
            if(j == v[i])f[i][j] = f[i-1][j] + 1;
            if(j > v[i])f[i][j] = f[i-1][j] + f[i-1][j-v[i]];
            if(j < v[i])f[i][j] = f[i-1][j];
        }   
    }
    cout << f[n][m];
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/wlw-x/p/12467093.html