首页 > 其他 > 详细

UVa LA 4636 Cubist Artwork 难度: 0

时间:2019-04-12 22:48:41      阅读:187      评论:0      收藏: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\n", ans);
    }
    return 0;
}
View Code

 

UVa LA 4636 Cubist Artwork 难度: 0

原文:https://www.cnblogs.com/xuesu/p/10699017.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!