poj 3802 Cubist Artwork

来源:互联网 发布:淘宝网天猫女鞋 编辑:程序博客网 时间:2024/05/22 17:22

就是堆方块之后,给出主视图和右视图,求最少用多少块方块可以满足题意。

就是用点贪心的想法,从大到小排序两个视图,从大到小,如果主视图和右视图出现了一样的,那么就说明主视图和右视图可以只用这一个块,余下的块每个都要用一次。

不知道为什么这么简单的题为什么比赛的时候一点思路也没有,唉。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 15;
int w,d;
int a[maxn],b[maxn];
bool  cmp(int a,int b){
    return a>b;
}
int main()

    while(scanf("%d%d",&w,&d),w||d){
        for(int i=0;i<w;++i){
            scanf("%d",&a[i]);
        }
        for(int i=0;i<d;++i){
            scanf("%d",&b[i]);
        }
        sort(a,a+w,cmp);
        int ans = 0;
        for(int i=0;i<w;++i){
            for(int j=0;j<d;++j){
                if(a[i]==b[j]){
                    ans+=a[i];
                    a[i]=0;
                    b[j]=0;
                }
            }
        }
        for(int i=0;i<w;++i) ans+=a[i];
        for(int i=0;i<d;++i) ans+=b[i];
        printf("%d\n",ans);
    }
    return 0;
}

0 0
原创粉丝点击