Question Link
Question: "N" indicates the side length of the overall bottom area built with the building blocks. Then, the front view and the right view are given respectively, you need to find the difference between the minimum number of blocks and the maximum number of blocks.
Train of Thought: in fact, the question is that you need to find the minimum number of blocks and the maximum number of blocks, we can solve them separately.
First, for the minimum number of blocks, if you want to build the cube with the minimum number, it means that the height of each vertical cube in the positive view is best utilized by the height in the right view. Therefore, we use the positive view as the benchmark. The total number of cubes required by the positive view plus the number of positive views that cannot be used in the lateral view is the minimum number of cubes required. Secondly, for the maximum number of blocks, we also take the positive view as the benchmark, and then compare with the right view, one layer calculates the number of blocks, each layer can be filled as much as possible, and then the accumulation is the maximum number of blocks.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 10;int a[MAXN], b[MAXN], num1[MAXN], num2[MAXN];int n;int getMin() { memset(num1, 0, sizeof(num1)); memset(num2, 0, sizeof(num2)); for (int i = 1; i <= n; i++) { num1[a[i]]++; num2[b[i]]++; } int sum = 0; for (int i = 1; i <= MAXN; i++) sum += max(num1[i], num2[i]) * i; return sum;}int getMax() { int cnt1, cnt2, sum = 0; while (1) { cnt1 = 0; for (int i = 1; i <= MAXN; i++) if (a[i]) { cnt1++; a[i]--; } cnt2 = 0; for (int i = 1; i <= MAXN; i++) if (b[i]) { cnt2++; b[i]--; } if (!cnt1 && !cnt2) break; sum += cnt1 * cnt2; } return sum;}int main() { int cas; scanf("%d", &cas); while (cas--) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); int Min = getMin(); int Max = getMax(); printf("Matty needs at least %d blocks, and can add at most %d extra blocks.\n", Min, Max - Min); } return 0;}