UVa 434 - Matty's Blocks 解题报告(水题)

434 - Matty's Blocks

Time limit: 3.000 seconds

 Matty's Blocks 

Little Matty liked playing with his blocks very much. He always constructed his `buildings' in the same way: he made stacks of one or more blocks, and he put those stacks on a square table that was exactly K blocks wide (K = 8 for his largest table, so it could contain up to 8 tex2html_wrap_inline101 8 block stacks). He didn't put the stacks randomly on the table. No, he always made a nice `square' pattern. In most buildings, there was no pattern visible in the heights of the stacks. However, since Little Matty himself was only eight blocks tall, a single stack of blocks never consisted of more than eight blocks.

This is an example of one of his buildings. It was built on a table that could contain 4 tex2html_wrap_inline101 4 block stacks.

He liked drawing too. To record his block buildings for future generations, he would draw them on paper. Since drawing a three-dimensional block building just was too hard for him, he made two drawings of a building: one straight from the front (you could only see the front of the blocks), and one from the right (you could only see the right side of the blocks). The drawings were in fact two-dimensional projections of the block building, showing only its outline on the front or on the right side.

These are the drawings he made of the building shown above.

                          Front                                Right side

He thought that such a pair of drawings would give enough information to be able to re-build the block building later (but he never tried it).

Years later, looking again at the drawings, he realized that this was not the case: from most pairs of drawings, he was able to construct more than one building that had the same outlines (front and right side) as the original one. He found out that from every (front, side)-pair of drawings that he had made, he could construct a `minimal' building, one that has the same outlines as the original building and contains a minimal number of blocks N (so it was not possible to construct a building with the same outlines with less than N blocks). Furthermore, he could add more blocks to this minimal building, so that the outlines remained the same, up to the point that he had added M () blocks and he had a `maximal' building, one that still had the same outlines as his minimal one, and adding one extra block would result in a building with a different outline (so there are no buildings with the same outlines that contain more than N + M blocks).

As an example, these are minimal and maximal buildings for the drawings shown above. In this case, N = 7 and M = 10.

                 Minimal Building                            Maximal Building

Matty started determining the N and M for every pair of drawings he had made, but soon he found this task to be tedious. Now he asks you to write a program that does the job for him!

Input Specification

The input contains on the first line the number of test cases. Each test case starts with a line containing only the size of the table K. The next pair of lines each contain the description of one drawing. Each description consists of K non-negative integers separated by spaces. Each number indicates the height of the corresponding projection of a stack of blocks in the drawing. The description of the front drawing always precedes the description of the right side drawing. From each pair of drawings at least one block building can be constructed.

Output specification

For each test case output the following line:

Matty needs at least N blocks, and can add at most M extra blocks.

Sample Input

2
4
2 0 3 1
1 1 2 3
1
1
1

Sample Output

Matty needs at least 7 blocks, and can add at most 10 extra blocks.
Matty needs at least 1 blocks, and can add at most 0 extra blocks.

    解题报告: 水题。

    最少多少个:统计高度为h的左视图的列有多少,正视图有多少,那么最少有它们中较大值的列高度为h。全部加起来即可。

    最多多少个:画图分析,答案是∑ min(a[i], b[j]), 1<= i, j <= n。

    代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define bit(n) (1LL<<(n))
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
    freopen("in.txt", "r", stdin);
#endif // ACM
    work();
}

/***************************************************/

int v[20], h[20];
int a[20], b[20];

void work()
{
    int T;
    scanf("%d", &T);
    fff(cas, 1, T)
    {
        memset(v, 0, sizeof(v));
        memset(h, 0, sizeof(h));

        int n;
        scanf("%d", &n);
        ff(i, n) scanf("%d", a+i), v[a[i]]++;
        ff(i, n) scanf("%d", b+i), h[b[i]]++;

        int minB = 0;
        fff(i, 1, 8)
            minB += max(v[i], h[i])*i;

        int maxB = 0;
        ff(i, n) ff(j, n)
            maxB += min(a[i], b[j]);

        printf("Matty needs at least %d blocks, and can add at most %d extra blocks.\n", minB, maxB - minB);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值