zoukankan      html  css  js  c++  java
  • UVA434

    题目链接


    题意:给出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.
    ", Min, Max - Min);
        }
        return 0;
    }


  • 相关阅读:
    scala之伴生对象的继承
    scala之伴生对象说明
    “Failed to install the following Android SDK packages as some licences have not been accepted” 错误
    PATH 环境变量重复问题解决
    Ubuntu 18.04 配置java环境
    JDBC的基本使用2
    DCL的基本语法(授权)
    ZJNU 1374
    ZJNU 2184
    ZJNU 1334
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4590694.html
Copyright © 2011-2022 走看看