inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[507][507], a[507], s[507];
intmain(){ int n = rd(); for (int i = 1; i <= n; ++i) { a[i] = rd(); s[i] = s[i - 1] + a[i]; } memset(f, 127, sizeof(f)); for (int i = 1; i <= n; ++i) f[i][i] = 0; for (int i = 1; i < n; ++i) { for (int j = 1; j <= n - i; ++j) { for (int k = j; k < j + i; ++k) { f[j][j + i] = min(f[j][j + i], f[j][k] + f[k + 1][j + i] + s[j + i] - s[j - 1]); } } } printf("%d\n", f[1][n]); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[507][507], a[507], s[507];
intmain(){ int n = rd(); for (int i = 1; i <= n; ++i) { a[i] = a[i + n] = rd(); } n *= 2; for (int i = 1; i <= n; ++i) { s[i] = s[i - 1] + a[i]; } memset(f, 127, sizeof(f)); for (int i = 1; i <= n; ++i) f[i][i] = 0; for (int i = 1; i < n; ++i) { for (int j = 1; j <= n - i; ++j) { for (int k = j; k < j + i; ++k) { f[j][j + i] = min(f[j][j + i], f[j][k] + f[k + 1][j + i] + s[j + i] - s[j - 1]); } } } int ans = 1 << 30; for (int i = 1; i <= n/2; ++i) ans = min(ans, f[i][i + n/2 - 1]); printf("%d\n", ans); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
int f[507][507]; char s[507];
intmain(){ int n = rd(); scanf("%s", s + 1); memset(f, 0, sizeof(f)); for (int i = 1; i < n; ++i) { for (int j = 1; j <= n - i; ++j) { if ((s[j] == '(' && s[j + i] == ')') || (s[j] == '[' && s[j + i] == ']')) f[j][j + i] = f[j + 1][j + i - 1] + 2; for (int k = j; k < j + i; ++k) f[j][j + i] = max(f[j][j + i], f[j][k] + f[k + 1][j + i]); } } printf("%d\n", f[1][n]); return0; }
树型DP
统计人数
一家公司里有 个员工,除了公司 CEO 外,每个人都有一个直接上司。我们想知道,每个人的团队(包括他/她自己、他的直接下属和间接下属)一共有多少人
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
structnode { node *nxt; int where; } *first[100001], a[100001];
inlinevoidsolve(int i){ f[i] = 1; for (node *x = first[i]; x; x = x -> nxt) { solve(x -> where); f[i] += f[x -> where]; } }
intmain(){ n = rd(); memset(first, 0, sizeof(first)); l = 0; for (int i = 2; i <= n; ++i) { int x; scanf("%d", &x); makelist(x, i); } solve(1); for (int i = 1; i <= n; ++i ){ printf("%d ", f[i]); } return0; }
没有上司的舞会
一家公司里有 个员工,除了公司 CEO 外,每个人都有一个直接上司。今天公司要办一个舞会,为了大家玩得尽兴,如果某个员工的直接上司来了,他/她就不想来了。第 个员工来参加舞会会为大家带来 点快乐值。请求出快乐值最大是多少。
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
structnode { node *nxt; int where; } *first[100001], a[100001];
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
structnode { node *nxt; int where; } *first[100001], a[100001];
inlinevoidsolve(int i){ for (node *x = first[i]; x; x = x -> nxt) { solve(x -> where); for (int j = m; j >= 0; --j) for (int k = 0; k <= j; ++k) { f[i][j][0] = max(f[i][j][0], f[i][j - k][0] + max(f[x -> where][k][0], f[x -> where][k][1])); f[i][j][1] = max(f[i][j][1], f[i][j - k][1] + f[x -> where][k][0]); } } for (int j = m; j; --j) f[i][j][1] = f[i][j - 1][1] + v[i]; f[i][0][1] = 0; }
intmain(){ n = rd(); m = rd(); memset(first, 0, sizeof(first)); l = 0; for (int i = 2; i <= n; ++i) { int x; scanf("%d", &x); makelist(x, i); } for (int i = 1; i <= n; ++i) { v[i] = rd(); } solve(1); printf("%lld\n", max(f[1][m][0], f[1][m][1])); return0; }
Post title:区间DP && 树形DP
Post author:Eva.Q
Create time:2022-01-26 10:19:07
Post link:https://qyy/2022/01/26/Algorithm/DP2/
Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.