博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
“访问”美术馆
阅读量:4981 次
发布时间:2019-06-12

本文共 1070 字,大约阅读时间需要 3 分钟。

树形dp

dp[i][j]=max(dp[i][j],dp[lch][k]+dp[rch][j-k])

#include
using namespace std;int t, n, m, dp[1005][605];struct node { int tim, pic; }tr[5005];inline int read() { int red = 0, f_f = 1; char ch = getchar(); while(ch>'9' || ch <'0') {if(ch=='-') f_f = -1; ch = getchar();} while(ch>='0' &&ch<='9') red = red*10+ch-'0', ch = getchar(); return red*f_f;}void dfs_read(int a) { tr[a].tim = read() * 2, tr[a].pic = read(); if(!tr[a].pic) { dfs_read(a << 1); dfs_read(a << 1| 1); }}void dfs(int a, int r) { if(dp[a][r] || !r) return; if(tr[a].pic) { dp[a][r] = min(tr[a].pic, (r - tr[a].tim) / 5); return; } for (int i = 0; i <= r - tr[a].tim; i++) { dfs(a << 1, i); dfs(a << 1 | 1, r - tr[a].tim - i); dp[a][r] = max(dp[a][r], dp[a << 1][i] + dp[a << 1 | 1][r - tr[a].tim - i]); }}int main(){ memset(dp, 0, sizeof(dp)); t = read() - 1; dfs_read(1); dfs(1, t); printf("%d\n", dp[1][t]); return 0;}

  

转载于:https://www.cnblogs.com/ainiyuling/p/11200700.html

你可能感兴趣的文章