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

题目传送门

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;
}
posted @ 2019-09-21 17:19  hankeke303  阅读(173)  评论(0编辑  收藏  举报