UVA1445 LA4636 POJ3802 Cubist Artwork贪心

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA1445 LA4636 POJ3802 Cubist Artwork贪心相关的知识,希望对你有一定的参考价值。

Cubist Artwork
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 477 Accepted: 308

Description

International Center for Picassonian Cubism is a Spanish national museum of cubist artworks, dedicated to Pablo Picasso. The center held a competition for an artwork that will be displayed in front of the facade of the museum building. The artwork is a collection of cubes that are piled up on the ground and is intended to amuse visitors, who will be curious how the shape of the collection of cubes changes when it is seen from the front and the sides.

The artwork is a collection of cubes with edges of one foot long and is built on a flat ground that is divided into a grid of one foot by one foot squares. Due to some technical reasons, cubes of the artwork must be either put on the ground, fitting into a unit square in the grid, or put on another cube in the way that the bottom face of the upper cube exactly meets the top face of the lower cube. No other way of putting cubes is possible.

You are a member of the judging committee responsible for selecting one out of a plenty of artwork proposals submitted to the competition. The decision is made primarily based on artistic quality but the cost for installing the artwork is another important factor. Your task is to investigate the installation cost for each proposal. The cost is proportional to the number of cubes, so you have to figure out the minimum number of cubes needed for installation.

Each design proposal of an artwork consists of the front view and the side view (the view seen from the right-hand side), as shown in Figure 1.

The front view (resp., the side view) indicates the maximum heights of piles of cubes for each column line (resp., row line) of the grid.

There are several ways to install this proposal of artwork, such as the following figures.

In these figures, the dotted lines on the ground indicate the grid lines. The left figure makes use of 16 cubes, which is not optimal. That is, the artwork can be installed with a fewer number of cubes. Actually, the right one is optimal and only uses 13 cubes. Note that, a single pile of height three in the right figure plays the roles of two such piles in the left one.

Notice that swapping columns of cubes does not change the side view. Similarly, swapping rows does not change the front view. Thus, such swaps do not change the costs of building the artworks.

For example, consider the artwork proposal given in Figure 2.

An optimal installation of this proposal of artwork can be achieved with 13 cubes, as shown in the following figure, which can be obtained by exchanging the rightmost two columns of the optimal installation of the artwork of Figure 1.

Input

The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. Each dataset is formatted as follows.

w d
h1 h2 … hw
h1’ h2’ … hd’

The integers w and d separated by a space are the numbers of columns and rows of the grid, respectively. You may assume 1 <= w <= 10 and 1 <= d <= 10. The integers separated by a space in the second and third lines specify the shape of the artwork. The integers hi (1 <= hi <= 20, 1 <= i <= w) in the second line give the front view, i.e., the maximum heights of cubes per each column line, ordered from left to right (seen from the front). The integers hi’ (1 <= hi’ <= 20, 1 <= i <= d) in the third line give the side view, i.e., the maximum heights of cubes per each row line, ordered from left to right (seen from the right-hand side).

Output

For each dataset, output a line containing the minimum number of cubes. The output should not contain any other extra characters.

You can assume that, for each dataset, there is at least one way to install the artwork.

Sample Input

5 5
1 2 3 4 5
1 2 3 4 5
5 5
2 5 4 1 3
4 1 5 3 2
5 5
1 2 3 4 5
3 3 3 4 5
3 3
7 7 7
7 7 7
3 3
4 4 4
4 3 4
4 3
4 2 2 4
4 2 1
4 4
2 8 8 8
2 3 8 3
10 10
9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9
10 9
20 1 20 20 20 20 20 18 20 20
20 20 20 20 7 20 20 20 20
0 0

Sample Output

15
15
21
21
15
13
32
90
186

Source

Tokyo 2009

问题链接UVA1445 LA4636 POJ3802 Cubist Artwork
问题简述:用给定的立方体搭积木,每个立方体或放在地面的网格上,或放在另一个立方体上。给定正视图和左视图,求最少用多少个立方体搭建积木?
问题分析:用贪心法来解决,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA1445 LA4636 POJ3802 Cubist Artwork */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 20;
int w, d, h[N + 1], h1[N + 1];

int main()
{
    int a;
    while (~scanf("%d%d", &w, &d) && (w || d)) {
        memset(h, 0, sizeof h);
        memset(h1, 0, sizeof h1);

        for (int i = 1; i <= w; i++) {
            scanf("%d", &a);
            h[a]++;
        }
        for (int i = 1; i <= d; i++) {
            scanf("%d", &a);
            h1[a]++;
        }

        int ans = 0;
        for (int i = 1; i <= N; i++)
            ans += max(h[i], h1[i]) * i;

        printf("%d\\n", ans);
    }

    return 0;
}

以上是关于UVA1445 LA4636 POJ3802 Cubist Artwork贪心的主要内容,如果未能解决你的问题,请参考以下文章

HDU1416 POJ1078 UVA653 LA5507 GizilchDFS

UVA12081 LA3413 POJ2769 Reduced ID Numbers同余

LA 4636 积木艺术

UVA619 LA5465 POJ1312 HDU1314 ZOJ1272 Numerically Speaking大数+进制

UVA326 LA5434 POJ1538 Extrapolation Using a Difference Table二项式

UVA557 LA5578 Burger概率