题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=4007

https://loj.ac/problem/2111

题解

[NOI2006]网络收费,背包很显然,然后因为祖先的状态不确定对之的影响,直接枚举就可以了。

具体见 https://www.cnblogs.com/hankeke/p/bzoj1495.html。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} #define lc x << 1
#define rc x << 1 | 1 const int N = 2048 + 7;
const int INF = 0x7f7f7f7f; int n, m, mm;
int a[N], c[N], w[2][N][N], col[N], dp[N][N]; inline void dfs(int x) {
if (x >= m) {
dp[x][0] = dp[x][1] = 0;
for (int i = x >> 1; i; i >>= 1) dp[x][col[i]] += w[col[i]][x][i];
return;
}
int siz = 1 << (n - std::__lg(x));
memset(dp[x], 0, sizeof(int) * (siz + 1));
col[x] = 0, dfs(lc), dfs(rc);
for (int i = 0; i <= (siz >> 1); ++i)
for (int j = 0; j <= (siz >> 1); ++j)
smax(dp[x][i + j], dp[lc][i] + dp[rc][j]);
col[x] = 1, dfs(lc), dfs(rc);
for (int i = 0; i <= (siz >> 1); ++i)
for (int j = 0; j <= (siz >> 1); ++j)
smax(dp[x][i + j], dp[lc][i] + dp[rc][j]);
} inline void work() {
dfs(1);
int ans = 0;
for (int i = 0; i <= mm; ++i) smax(ans, dp[1][i]);
printf("%d\n", ans);
} inline void init() {
read(n), read(mm), m = 1 << --n;
for (int i = 1; i <= m; ++i) {
int x = i + m - 1;
for (int j = x >> 1; j; j >>= 1) read(w[1][x][j]);
}
for (int i = 1; i <= m; ++i) {
int x = i + m - 1;
for (int j = x >> 1; j; j >>= 1) read(w[0][x][j]);
}
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

bzoj4007 & loj2111 [JLOI2015]战争调度 复杂度分析+树上背包的更多相关文章

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

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

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

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

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

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

  4. bzoj1495 [NOI2006]网络收费 复杂度分析+树上背包

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1495 题解 通过观察可以发现,对于一个 \(lca\),如果 \(nA \leq nB\),那 ...

  5. [JLOI2015]战争调度

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

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

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

  7. BZOJ4007 [JLOI2015]战争调度

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

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

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

  9. 【题解】JLOI2015战争调度

    搜索+状压+DP. 注意到一个性质:考虑一棵以x为根的子树,在x到原树的根的路径上的点如果都已经确定了方案,那么x的左右儿子的决策就彼此独立,互不影响了.所以我们考虑状压一条路径上每一层节点的状态,求 ...

随机推荐

  1. jsp文件断点上传

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

  2. Harbor修改暴露端口

    把原来的端口映射改成1180 一 修改docker-compose.yml [root@topcheer ~]# cat /mnt/harbor/docker-compose.yml version: ...

  3. 【CF1257F】Make Them Similar【meet in the middle+hash】

    题意:给定n个数,让你给出一个数,使得n个数与给出的数异或后得到的数的二进制表示中1的数量相同 题解:考虑暴搜2^30去找答案,显然不可接受 显然可以发现,这是一个经典的meet in the mid ...

  4. Delphi实现获取磁盘空间大小的方法

    unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ...

  5. (最小割)Path

    http://acm.hdu.edu.cn/showproblem.php?pid=6582 思路:找到最短路核心边建图,跑一遍最小割,最短路核心边的定义为设起点到每个点的最短距离为d[i],每个点到 ...

  6. (转)基于TLS证书手动部署kubernetes集群(上)

    转:https://www.cnblogs.com/wdliu/archive/2018/06/06/9147346.html 一.简介 Kubernetes是Google在2014年6月开源的一个容 ...

  7. Delphi 二维码生成

    Delphi 二维码生成 http://download.csdn.net/detail/warrially/7370171

  8. eval方法遇到的问题

    工作中有这样的场景,一个表达式比如 2*2,计算结果是number,这样的为true,如果输入错误 2*@,这样的情况需要匹配为false. 这里使用的eval方法, type of (eval('2 ...

  9. 常见的网络设备:集线器 hub、网桥、交换机 switch、路由器 router、网关 gateway

    Repeater 中继器 Hub 集线器 bridge 网桥 switch 交换机 router 路由器 gateway 网关 网卡 参考资料: do-you-know-the-differences ...

  10. Vagrant 手册之 Vagrantfile - Vagrant 设置 config.vagrant

    原文地址 配置的命名空间:config.vagrant config.vagrant 中的设置修改 Vagrant 自身的行为. 1. 可用设置 config.vagrant.host 设置运行 Va ...