UVA434 - Matty's Blocks
来源:互联网 发布:淘宝联盟加入购物车 编辑:程序博客网 时间:2024/06/06 23:50
题目链接
题意:给出n,代表所要用积木搭建的整体的底面积的边长,然后分别给出正视图和右视图,要你求出搭建都要形状的最小木块数量和最小木块数量和最大木块数量的差值。
思路:其实题目就是要你求出最小木块数和最大木块数,我们可以分开求解。
首先对于最小木块数,要想用最少的立方体搭建,那就意味着正视图中的每一竖立方体的高度最好都要被右视图中的高度所利用到。所以我们以正视图为基准,正视图需要的立方体总数加上侧视图存在无法利用正视图的数量,就是最少需要的立方体数。其次对于最大木块数,我们也以正视图为基准,再对照右视图,一层一层计算木块数,尽量每一层都能铺满,然后累加上去就是最大的木块数了。
#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;}
0 0
- UVA434 - Matty's Blocks
- UVA 434 Matty's Blocks
- UVA - 434 Matty's Blocks
- 【UVA】434-Matty's Blocks
- UVA 434 - Matty's Blocks(贪心)
- uva 434 - Matty's Blocks(贪心)
- UVA - 434 Matty's Blocks 逻辑
- uva 434 - Matty's Blocks(贪心)
- UVa 434 - Matty's Blocks 解题报告(水题)
- uva434(逻辑)
- Baby's Blocks Gym
- Plato's Blocks(DFS+模拟)
- 各种想得到的图mbostock’s blocks
- Ry’s Objective-C Tutorial---Blocks
- blocks
- Blocks
- Blocks
- Blocks
- Linux进阶——tar 解压缩命令
- Java如何通过WSDL文件来调用这些web service
- 32位win7系统下配置IIS遇到php-cgi.exe - FastCGI 进程意外退出问题的解决办法
- poj--2524
- hdu 4941 Magical Forest (map)
- UVA434 - Matty's Blocks
- poj-3225
- 一个简单的数字记忆训练软件介绍
- (14)Android监听时return 返回的false与true的区别
- lsnrctl 中 关于status 状态的说明
- LeetCode-Maximum Subarray
- axis2 jar包详解及缺少jar包错误分析
- 【LeetCode】LRU Cache
- UITableView tableHeaderView touch