A
暴力按值排序后取出最后一位运算即可
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
char a[maxn], b[maxn];
bool cmp(char x, char y) {
return x > y;
}
int main()
{
//freopen("in.txt", "r", stdin);
scanf("%s", &a);
strcpy(b, a);
int n = strlen(a);
sort(b, b + n, cmp);
int ans = (b[n - 1] - a[n - 1] + 10) % 10;
printf("%d\n", ans);
}
B
最优解问题dp,我们需要考虑的是每一天的状态为当天结束时拥有的钱数量
dp[i][j]表示i天结束时拥有j的金钱时获得的最大收益,则对每个物品做一次完全背包,注意标记不能转移的状态
最后的答案即是剩下的钱+收益
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 5;
int f[100005], tmp[100005], b[maxn];
int dp[maxn][100005];
map<int, int> a;
int main(int argc, char const *argv[])
{
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false); cin.tie(0);
int n, m, k; cin >> n >> m >> k;
for (int i = 0; i <= k; ++i)
cin >> f[i];
int tot = 0;
for (int i = 1; i <= m; ++i) {
int x, y; cin >> x >> y;
if (a.count(x)) a[x] = max(a[x], y);
else b[tot++] = x, a[x] = y;
}
sort(b, b + tot);
memset(dp, -1, sizeof(dp));
dp[1][f[0]] = 0;
for (int i = 2; i <= n; ++i) {
for (int j = 0; j <= 2000; ++j)
tmp[j] = dp[i - 1][j];
for (int j = 2000; j >= 0; --j) {
if (tmp[j] == -1) continue;
for (int k = 0; k < tot; ++k) {
int x = b[k], y = a[b[k]];
if (j < x) continue;
tmp[j - x] = max(tmp[j] + y, tmp[j - x]);
}
}
for (int j = 0; j <= 2000; ++j)
if (tmp[j] != -1) dp[i][j + f[j]] = max(dp[i][j + f[j]], tmp[j]);
}
int ans = 0;
for (int i = 0; i <= 2000; ++i)
if (dp[n][i] != -1) ans = max(ans, i + dp[n][i]);
cout << ans << endl;
}
D
克鲁斯卡尔重构树模板题
https://blog.csdn.net/weixin_40588429/article/details/101351599