Question Link
Question: Use a number of other large cubes to build blocks. Each cube can be directly placed on a grid on the ground, or placed on another cube to provide a positive view and a lateral view, find the minimum number of cubes to build.
Idea: to build a cube with the least amount, it means that the height wh of each vertical cube seen on the front is best utilized by the height DH seen on the side. Therefore, we use the front as the benchmark. The total number of cubes required by the front plus the number of cubes required by the DH on the side cannot use the front Wh, which is the minimum number of cubes required.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 100;int a[MAXN], b[MAXN], num1[MAXN], num2[MAXN];int main() { int w, d; while (scanf("%d%d", &w, &d) && w && d) { memset(num1, 0, sizeof(num1)); memset(num2, 0, sizeof(num2)); for (int i = 1; i <= w; i++) { scanf("%d", &a[i]); num1[a[i]]++; } for (int i = 1; i <= d; i++) { scanf("%d", &b[i]); num2[b[i]]++; } int sum = 0; for (int i = 1; i <= MAXN; i++) sum += max(num1[i], num2[i]) * i; printf("%d\n", sum); } return 0;}