牛客挑战赛66

https://ac.nowcoder.com/acm/contest/50846

A.优美数字

直接按照题意模拟即可 求出rev(x)

B.树上赋值

分析:

C.上班

分析

具体代码 先将走到<n,m>的路径提出来

对于其他分支 用背包去跟新他 背包开一个新数组方便转移

对于主干上 用得到的每个分支的最优解 去跟新dp数组 同样也是用到背包

#include <bits/stdc++.h>
#define N 10100
#define pb push_back
using namespace std;
typedef long long ll;

int n, m, t, a[N];
vector<vector<int> > id;
bool ban[N][N];
vector<int> E[N]; 
int fa[N], siz[N], on[N], SZ;
vector<vector<ll> > f; 
ll tmp[N], dp[N];

void dfs1(int x) {
	siz[x] = 1;
	for (auto y : E[x]) if (y != fa[x]) {
		fa[y] = x; dfs1(y); siz[x] += siz[y];
	}
}
void dfs2(int x) {
	if (!on[x]) {
		f[x].resize(siz[x]+1);
		f[x][1] = a[x]; 
	}
	int sz = 1;
	for (auto y : E[x]) if (y != fa[x]) {
		dfs2(y);
		if (on[x]) continue;
		for (int i = 0; i <= sz+siz[y]; i++) tmp[i] = 0;
		for (int i = 1; i <= sz; i++) for (int j = 0; j <= siz[y]; j++) {
			tmp[i+j] = max(tmp[i+j], f[x][i]+f[y][j]);
		}
		for (int i = 0; i <= sz+siz[y]; i++) f[x][i] = tmp[i];
		sz += siz[y];
	}
	if (!on[x] && on[fa[x]]) {
		for (int i = 0; i <= SZ+siz[x]; i++) tmp[i] = 0;
		for (int i = 0; i <= SZ; i++) for (int j = 0; j <= siz[x]; j++) {
			tmp[i+j] = max(tmp[i+j], dp[i]+f[x][j]);
		}
		for (int i = 0; i <= SZ+siz[x]; i++) dp[i] = tmp[i];
		SZ += siz[x];
	}
}

int main() {
	scanf("%d %d %d", &n, &m, &t);
	id.resize(n+1);
	int tot = 0;
	for (int i = 0; i <= n; i++) {
		id[i].resize(m+1);
		for (int j = 0; j <= m; j++) {
			id[i][j] = ++tot;
			scanf("%d", &a[tot]);
		}
	}
	for (int i = 1; i <= n*m; i++) {
		int p, q, x, y;
		scanf("%d %d %d %d", &p, &q, &x, &y);
		ban[id[p][q]][id[x][y]] = ban[id[x][y]][id[p][q]] = 1;
	}
	for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) {
		int x = id[i][j];
		if (i < n && !ban[x][id[i+1][j]]) E[x].pb(id[i+1][j]), E[id[i+1][j]].pb(x);
		if (j < m && !ban[x][id[i][j+1]]) E[x].pb(id[i][j+1]), E[id[i][j+1]].pb(x);
	}
	dfs1(1);
	for (int x = id[n][m]; x; x = fa[x]) {
		on[x] = 1; --t;
		dp[0] += a[x]; 
	}
	++t; f.resize(tot+1);
	dfs2(1);
	t = min(t/2, SZ);
	printf("%lld\n", dp[t]);
	return 0;
}

D.排列

分析:

posted @ 2023-03-06 18:59  wzx_believer  阅读(53)  评论(0编辑  收藏  举报