Luogu P1270_tree dp_dfs+ memorized search

#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 1000;

struct treenode
{
    int time, val;
}tn[maxn*10];

int pt;//The time the police arrived
int dp[maxn][maxn]={0};//At the i node, the remaining time is the maximum value of j (just now)

int dfs(int rt, int tt);
void gettree(int rt);

intmain()
{
    scanf("%d", &pt);
    gettree(1);
    int ans = dfs(1, pt-1);
    printf("%d\n", ans);
    return 0;
}

int dfs(int rt, int tt)//The node of the tree, the current remaining time, the memory search
{
    if(dp[rt][tt] || !tt) return dp[rt][tt];//Memoize! !
    int tmpt = tt - tn[rt].time;
    if(tn[rt].val && tmpt) return dp[rt][tt] = min(tn[rt].val, tmpt/5);
    for(int i = 0; i <= tmpt; ++i)//The time to enumerate the subtrees on both sides
    {
        int tmp = dfs(rt<<1, i)+dfs(rt<<1|1, tmpt-i);
        dp[rt][tt] = max(dp[rt][tt], tmp);
    }
    return dp[rt][tt];
}

void gettree(int rt)
{
    int t, v;
    scanf("%d %d", &t, &v);
    tn[rt].time = t*2;
    tn[rt].val = v;
    if(v) return;
    gettree(rt<<1);
    gettree(rt<<1|1);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325726043&siteId=291194637