题目大意:给出前视图和右视图,计算出最少需要几个正方体以及至多可再增加几个正方体。
解题思路:和昨天做得一题uva 1445一样的,只是增加了要计算说最多可以放几个正方体,贪心,尽量让大的当着小的。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 10;
int n, f[N], r[N];
void init() {
memset(f, 0, sizeof(f));
memset(r, 0, sizeof(r));
scanf("%d", &n);
int a;
for (int i = 0; i < n; i++) {
scanf("%d", &a); f[a]++;
}
for (int i = 0; i < n; i++) {
scanf("%d", &a); r[a]++;
}
}
void solve() {
int Min = 0, Max = 0, left = n, right = n;
for (int i = 0; i < N; i++) {
Min += max(f[i], r[i]) * i;
Max += left * right;
left -= f[i];
right -= r[i];
}
printf("Matty needs at least %d blocks, and can add at most %d extra blocks.\n", Min, Max - Min - n * n);
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
init();
solve();
}
return 0;
}