275
题意:
构造出一棵树,给出每种度数的权值,一棵树的权值是 所有点权和,求最大权值
Solution:
我们可以发现由于是一棵树,所有度数之和一定是2n?2,我们要做的其实就是度数的一个划分,使得权值最大而已,dp一下即可。
Code:
#include <bits/stdc++.h>
using namespace std;
const int N = 55;
int dp[N][N << 1];
class P8XGraphBuilder {
public:
int solve(vector <int> scores) {
int n = scores.size() + 1;
memset(dp, -1, sizeof(dp));
int m = 2 * n - 2;
dp[0][0] = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
for (int k = 1; k + j <= m; ++k)
if (~dp[i][j]) dp[i + 1][j + k] = max(dp[i + 1][j + k], dp[i][j] + scores[k - 1]);
return dp[n][m];
}
};
450
题意:
给你一个0/1矩阵,包含’?’,’?’可以变成0或1,给定每行的情况以及每列的情况,让你输出字典序最小的矩阵
Solution
这个题很裸,显然可以看出是二分图匹配,枚举问号,先枚举为0,跑匹配看是否为最大匹配,否则为1即可。
Code:
#include <bits/stdc++.h>
using namespace std;
const int N = 35;
int n, m, g[N][N], l[N], v[N];
bool check(vector <string> a, vector <string> b, int x, int y) {
for (int i = 0; i < n; ++i) {
char c = a[i][x], d = b[y][i];
if (c != d && c != ‘?‘ && d != ‘?‘) return 0;
}
return 1;
}
bool can(int u) {
for (int i = 0; i < m; ++i) {
if (g[u][i] && !v[i]) {
v[i] = 1;
if (l[i] == -1 || can(l[i])) {
l[i] = u;
return 1;
}
}
}
return 0;
}
bool ok() {
memset(l, -1, sizeof(l));
int cnt = 0;
for (int i = 0; i < m; ++i) {
memset(v, 0, sizeof(v));
if (can(i)) ++cnt;
}
return cnt == m;
}
class P8XMatrixRecovery {
public:
vector <string> solve(vector <string> rows, vector <string> columns) {
n = rows.size(), m = columns.size();
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
if (rows[i][j] != ‘?‘) continue;
rows[i][j] = ‘0‘;
memset(g, 0, sizeof(g));
for (int k = 0; k < m; ++k)
for (int l = 0; l < m; ++l)
if (check(rows, columns, k, l)) g[k][l] = 1;
if (!ok()) rows[i][j] = ‘1‘;
}
return rows;
}
};
时间: 2024-10-22 06:26:46