搜索+状压+DP。

  注意到一个性质:考虑一棵以x为根的子树,在x到原树的根的路径上的点如果都已经确定了方案,那么x的左右儿子的决策就彼此独立,互不影响了。所以我们考虑状压一条路径上每一层节点的状态,求出dp[u][x] : 以u为根的子树中分配x个作战平民的最大收益是多少(注意因为是在dfs当中,所以dp数组存的是在当前状况下的最优解)。

  代码挺短的,可食用~

#include <bits/stdc++.h>
using namespace std;
#define maxn 1025
int n, m, tot, ans, dp[maxn][maxn];
int w[maxn][], f[maxn][]; int read()
{
int x = , k = ;
char c;
c = getchar();
while(c < '' || c > '') { if(c == '-') k = -; c = getchar(); }
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * k;
} void dfs(int x, int y, int st, int cnt)
{
for(int i = ; i <= cnt; i ++) dp[x][i] = ;
if(y == n - )
{
int id = x - ( << y);
for(int i = ; i < y; i ++)
if(st & ( << i)) dp[x][] += w[id][i];
else dp[x][] += f[id][i];
return;
}
dfs(x << , y + , st | ( << y), cnt >> );
dfs(x << | , y + , st | ( << y), cnt >> );
for(int i = cnt >> ; ~i; i --)
for(int j = cnt >> ; ~j; j --)
dp[x][i + j] = max(dp[x][i + j], dp[x << ][i] + dp[x << | ][j]);
dfs(x << , y + , st, cnt >> );
dfs(x << | , y + , st, cnt >> );
for(int i = cnt >> ; ~i; i --)
for(int j = cnt >> ; ~j; j --)
dp[x][i + j] = max(dp[x][i + j], dp[x << ][i] + dp[x << | ][j]);
} int main()
{
n = read(), m = read();
tot = ( << (n - ));
for(int i = ; i < tot; i ++)
for(int j = n - ; ~j; j --)
w[i][j] = read();
for(int i = ; i < tot; i ++)
for(int j = n - ; ~j; j --)
f[i][j] = read();
dfs(, , , tot);
ans = ;
for(int i = ; i <= m; i ++) ans = max(ans, dp[][i]);
printf("%d\n", ans);
return ;
}

【题解】JLOI2015战争调度的更多相关文章

  1. 【BZOJ4007】[JLOI2015]战争调度(动态规划)

    [BZOJ4007][JLOI2015]战争调度(动态规划) 题面 BZOJ 洛谷 题解 神仙题,我是做不来. 一个想法是设\(f[i][j]\)表示当前考虑到\(i\)节点,其子树内有\(j\)个人 ...

  2. [JLOI2015]战争调度

    [JLOI2015]战争调度 题目 解题报告 考试打了个枚举的暴力,骗了20= = $qsy$大佬的$DP$: 其实就是枚举= =,只不过枚举的比较强= = #include<iostream& ...

  3. 【bzoj4007】[JLOI2015]战争调度 暴力+树形背包dp

    题目描述 给你一棵 $n$ 层的完全二叉树,每个节点可以染黑白两种颜色.对于每个叶子节点及其某个祖先节点,如果它们均为黑色则有一个贡献值,如果均为白色则有另一个贡献值.要求黑色的叶子节点数目不超过 $ ...

  4. 【bzoj4007】[JLOI2015]战争调度 暴力+树形dp

    Description 脸哥最近来到了一个神奇的王国,王国里的公民每个公民有两个下属或者没有下属,这种 关系刚好组成一个 n 层的完全二叉树.公民 i 的下属是 2 * i 和 2 * i +1.最下 ...

  5. bzoj4007 & loj2111 [JLOI2015]战争调度 复杂度分析+树上背包

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4007 https://loj.ac/problem/2111 题解 同 [NOI2006]网络 ...

  6. [JLOI2015]战争调度【暴力+树形Dp】

    Online Judge:Bzoj4007,Luogu P3262 Label:暴力,树形Dp 题解 参考了这篇blog https://www.cnblogs.com/GXZlegend/p/830 ...

  7. Luogu P3262 [JLOI2015]战争调度

    题意 给定一棵高度为 \(n\) 的完全二叉树,可以将节点设置成两种状态.如果某个叶子 \(x\) 的状态为 \(i\) 同时他的某个祖先也为 \(i\),那么这个叶子就会对祖先产生 \(f_{x,i ...

  8. BZOJ4007 [JLOI2015]战争调度

    根本想不出来... 原来还是暴力出奇迹啊QAQ 无限ymymym中 /************************************************************** Pr ...

  9. [BZOJ4007][JLOI2015]战争调度(DP+主定理)

    第一眼DP,发现不可做,第二眼就只能$O(2^{1024})$暴搜了. 重新审视一下这个DP,f[x][i]表示在x的祖先已经全部染色之后,x的子树中共有i个参战平民的最大贡献. 设k为总结点数,对于 ...

随机推荐

  1. 【linux运维递进】

    ================================云计算和虚拟化=================================== docker openstack svn git ...

  2. Plugin was not installed: Cannot download 'https://plugins.jetbrains.com/pluginManager''

    在Android studio中安装插件的时候,提示了类似这种的错误,解决这个问题有以下几步 1.打开Configure->Settings 2.System Settings->Upda ...

  3. python应用:爬虫实例(动态网页)

    以爬取搜狗图片为例,网页特点:采用“瀑布流”的方式加载图片,图片的真实地址存放在XHR中 #-*-coding:utf8-*- import requests import urllib import ...

  4. Qt——菜单栏、工具栏、状态栏

    1.菜单栏 菜单栏的意义是将可点击触发最终事件的集中在一起,所以菜单栏中是QAction 添加菜单栏是QMainWindow的行为 QMenubar *menubar = this->addMe ...

  5. nyoj 525 一道水题【字符串(分割)】

    参考:https://blog.csdn.net/dxx_111/article/details/48154687 #include <iostream> #include <cst ...

  6. Big Truck

    Photo by Phil Whitehouse Your boss has hired you to drive a big truck, transporting items between tw ...

  7. SIMD数据并行(二)——多媒体SIMD扩展指令集

    在计算机体系中,数据并行有两种实现路径:MIMD(Multiple Instruction Multiple Data,多指令流多数据流)和SIMD(Single Instruction Multip ...

  8. [Cracking the Coding Interview] 4.4 Check Balanced

    Implement a function to check if a binary tree is balanced. For the purpose of this question, a bala ...

  9. java web相对路径和绝对路径总结

    java web 开发过程中很多地方涉及url路径的问题,比如jsp页面.servlet之间的跳转.其实,可以将url中的/xxx看成一级目录,然后像看待目录层级之间的关系那样去看待url路径.接下来 ...

  10. ABAP CDS - Syntax

    The syntax of the DDL and of the DCL of the ABAP CDS comprises elements of the general DDL and DCL o ...