P1926 小书童——刷题大军
本题链接:P1926 小书童——刷题大军
本博客给出本题截图:
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 160, M = 15; int f[N]; // 在用时不超过 i 的情况下获得的最大得分 int v[M]; int vh[M], wh[M]; int main() { int n, m, k, r; cin >> n >> m >> k >> r; for (int i = 1; i <= n; i ++ ) cin >> v[i]; for (int i = 1; i <= m; i ++ ) cin >> vh[i]; for (int i = 1; i <= n; i ++ ) cin >> wh[i]; sort(v + 1, v + n + 1); // 贪心,优先去做耗时较少的题目 for (int i = 1; i <= n; i ++ ) for (int j = r; j >= vh[i]; j -- ) f[j] = max(f[j], f[j - vh[i]] + wh[i]); for (int i = 1; i <= r; i ++ ) if (f[i] >= k) { r -= i; break; } int cnt = 0; for (int i = 1; i <= n; i ++ ) if (r >= v[i]) { r -= v[i]; cnt ++; } cout << cnt << endl; return 0; }
P1802 5 倍经验日
本题链接:P1802 5 倍经验日
本博客给出本题截图:
#include <iostream> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; const int N = 1010; LL f[N]; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i ++ ) { int l, w, v; cin >> l >> w >> v; for (int j = m; j >= 0; j--) { if (j >= v) f[j] = max(f[j] + l, f[j - v] + w); else f[j] = f[j] + l; } } cout << 5 * f[m] << endl; return 0; }
P1734 最大约数和
本题链接:P1734 最大约数和
本博客给出本题截图:
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 1010; int w[N]; int f[N]; int main() { int n; cin >> n; // 预处理w数组 for (int i = 1; i <= n / 2; i ++ ) for (int j = 2; i * j <= n; j ++ ) w[i * j] += i; for (int i = 1; i <= n; i ++ ) for (int j = n; j >= i; j -- ) f[j] = max(f[j], f[j - i] + w[i]); cout << f[n] << endl; return 0; }
P2392 kkksc03考前临时抱佛脚
本题链接:P2392 kkksc03考前临时抱佛脚
本博客给出本题截图:
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int N = 610, M = 20; int s[5]; int w[M]; int f[N]; int main() { for (int i = 1; i <= 4; i ++ ) cin >> s[i]; int res = 0; for (int i = 1; i <= 4; i ++ ) { int sum = 0; memset(f, 0, sizeof f); for (int j = 1; j <= s[i]; j ++ ) cin >> w[j], sum += w[j]; int m = sum / 2; for (int j = 1; j <= s[i]; j ++ ) for (int k = m; k >= w[j]; k -- ) f[k] = max(f[k], f[k - w[j]] + w[j]); res += max(f[m], sum - f[m]); } cout << res << endl; return 0; }
总结
题目比较灵活,有时需要进行预处理,有时需要加上贪心的策略。