题意:
在一个n*n的方正上叠正方体;
现在给出前视图,和右视图;
问最少几个方块,还有最多在加几个方块(也就是最多减最少);
思路:
最少的很好求;
最小值的话,两个视图,存在高度一样的,就只取一次,高度不一样就两个都取;
最大值的话,先看前视图,并假设前视图后面堆满了方块;
然后看右视图,右视图中的每一个都要去前视图把多余的高度减掉;
例如
前视图的2 3 0 1;
右视图如果有一个1
那么2的要减1,3的要减2,0,1的不变;
如果有一个2;
那么3的要减1,0,1,2的都不变;
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N = 10;
int n;
int front[N],right[N],f0,r0;;
int cnt[N],vis[N];
int main() {
int t;
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
f0 = r0 = 0;
int Min = 0;
int Max = 0;
memset(cnt, 0 ,sizeof(cnt));
for(int i = 0; i < n; i++) {
scanf("%d",&front[i]);
cnt[front[i]]++;
Min += front[i];
Max += front[i] * n;
}
for(int i = 0; i < n; i++) {
scanf("%d",&right[i]);
cnt[right[i]]--;
for(int j = 0 ; j < n; j++) {
if(front[j] > right[i]) {
Max -= (front[j] - right[i]);
}
}
}
for(int i = 0; i <= 8; i++) {
if(cnt[i] < 0)
Min += (-cnt[i] * i);
}
printf("Matty needs at least %d blocks, and can add at most %d extra blocks.\n",Min, Max - Min);
}
}