题意:给你正视和侧视图,求最多多少个,最少多少个
思路:贪心的思想,求最少的时候:因为可以想象着移动,尽量让两个视图的重叠,所以我们统计每个视图不同高度的个数,然后计算,至于的话,就是每次拿正视图的高度去匹配侧视求最大
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int MAXN = 1000; int k; int view[2][MAXN]; int main() { int t; scanf("%d", &t); while (t--) { scanf("%d", &k); memset(view, 0, sizeof(view)); for (int i = 0; i < 2; i++) for (int j = 0; j < k; j++) { int x; scanf("%d", &x); view[i][x]++; } int Min = 0, Max = 0; for (int i = 1; i < MAXN; i++) Min += i * max(view[0][i], view[1][i]); for (int i = 1; i < MAXN; i++) for (int j = 1; j < MAXN; j++) Max += min(i, j)*view[0][i]*view[1][j]; printf("Matty needs at least %d blocks, and can add at most %d extra blocks.\n", Min, Max-Min); } return 0; }
UVA - 434 Matty's Blocks
时间: 2024-11-08 22:36:27