跳到正文

题解

题解 P1270 【“访问”美术馆】

用户 20833约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
P1270

版本

  1. v2当前
  2. v1

/* 基础树形dp 有依赖性背包问题

f[i][j]表示当前节点为i用掉j秒所取得的最大值

转移的时候 如果当前节点是子节点,就判断能取多少

如果不是就枚举当前节点所分配给左树的时间,由左右子树的和转移来。

*/

C++
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 1001
using namespace std;
int n,cnt,tot;
int f[maxn][maxn];
void dfs()
{
    int root=++cnt,limit,time;
    scanf("%d%d",&limit,&tot);
    limit<<=1;
    if(tot)//子节点
    {
        for(int time=limit;time<=n;time++)
          f[root][time]=min((time-limit)/5,tot);//判断取多少
    }
    else
    {
        int left=cnt+1,right;dfs();
        right=cnt+1;dfs();
        for(int time=limit;time<=n;time++)
          for(int lctime=0;lctime<=time-limit;lctime++)//分配给左树的时间
          {
              f[root][time]=max(f[root][time],f[left][lctime]+f[right][time-limit-lctime]);//左右子树的和
          }
    }
}
int main()
{
    scanf("%d",&n);n--;
    dfs();
    printf("%d\n",f[1][n]);
    return 0;
}
原文