码迷,mamicode.com
首页 > 其他好文 > 详细

UVA434 - Matty's Blocks

时间:2014-08-14 01:36:37      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   io   for   ar   amp   

题目链接


题意:给出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;
}


UVA434 - Matty's Blocks,布布扣,bubuko.com

UVA434 - Matty's Blocks

标签:style   http   color   os   io   for   ar   amp   

原文地址:http://blog.csdn.net/u011345461/article/details/38544943

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!