UVa LA 4636 Cubist Artwork 难度: 0

题目

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2637


题意

积木,有左视图和前视图,问最少几块积木

思路

明显,把前视图视作列,左视图视作行,从大到小排列行和列,如果此时未处理的行列最大值恰巧相等为h,那么就是说在这个新行/列中,恰可以放一个高为h的积木。如果不相等且较大值为h,那么就必须要做一个高为h的积木组,假如h是左视图上的要求,那么要把它放在列长大于等于h的列中掩盖起来,防止前视图中看到这个h高积木,因为前视图看不到这个h高积木。

简而言之,排个序,相同的只加一份,不同的各算一份。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 1e3 + 3;
typedef pair<int, int> Pair;
int n, m;
int a[MAXN], b[MAXN];
int main() {
    int T;
    //scanf("%d", &T);
    freopen("C:\Users\Iris\source\repos\ACM\ACM\input.txt", "r", stdin);
    //freopen("C:\Users\Iris\source\repos\ACM\ACM\output.txt", "w", stdout);
    for (int ti = 1; scanf("%d%d", &n, &m) == 2 && n && m; ti++) {
        for (int i = 0; i < n; i++) {
            scanf("%d", a + i);
        }
        sort(a, a + n);
        for (int i = 0; i < m; i++) {
            scanf("%d", b + i);
        }
        sort(b, b + m);
        int ans = 0;
        for (int i = n - 1, j = m - 1; i >= 0 || j >= 0;) {
            if (i < 0) {
                ans += b[j--];
            }
            else if (j < 0) {
                ans += a[i--];
            }
            else {
                if (a[i] == b[j]) {
                    ans += a[i];
                    i--; j--;
                }
                else if (a[i] > b[j]) {
                    ans += a[i--];
                }
                else {
                    ans += b[j--];
                }
            }
        }
        printf("%d
", ans);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/xuesu/p/10699017.html