UVA1445 Cubist Artwork 题解

· · 题解

UVA1445 Cubist Artwork 题解

思路分析

我们可以是使每一个行和列一一相同高度的配对,例如:

5 4 1 3 3
7 9 2 3 4

其中的 3 和 4 成功匹配,所以和减去 3 + 4:

5 + 4 + 1 + 3 + 3 + 7 + 9 + 2 + 3 + 4 - 3 - 4 = 34

开桶打标记实现。

代码实现

#include <cstdio>
#include <unordered_map>
constexpr int MaxN = 1e2 + 5;
int w, d;
int answer;
int p[MaxN];
std::unordered_map<int, int> cnt;
int main()
{
    for (;;)
    {
        scanf("%d%d", &w, &d);
        if (w == 0 && d == 0)
        {
            break;
        }
        for (int i = 1; i <= w; i++)
        {
            scanf("%d", &p[i]);
            cnt[p[i]]++;
            answer += p[i];
        }
        for (int i = 1; i <= d; i++)
        {
            scanf("%d", &p[i]);
            if (cnt[p[i]] >= 1)
            {
                cnt[p[i]]--;
            }
            else
            {
                answer += p[i];
            }
        }
        printf("%d\n", answer);
        answer = 0;
        cnt.clear();
    }
    return 0;
}