Stamps and Envelope Size

输入输出格式

输入格式:

输出格式:

输入输出样例

输入样例#1: 复制
5
2
4 1 4 12 21
4 1 5 12 28
10
2
5 1 7 16 31 88
5 1 15 52 67 99
6
2
3 1 5 8
4 1 5 7 8
0
输出样例#1: 复制
max coverage = 71 : 1 4 12 21
max coverage = 409 : 1 7 16 31 88
max coverage = 48 : 1 5 7 8

背包,暴力枚举就行
注意输出格式
#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e3+10;

int n,s,f[maxn],a[maxn],b[maxn];

int ans,minlen,maxa;

int main() {
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&s)&&s) {
        scanf("%d",&n);
        ans=0;
        while(n--) {
            memset(f,0,sizeof(f));
            f[0]=1;
            scanf("%d",&a[0]);
            for(int i=1;i<=a[0];i++) scanf("%d",a+i);
            int ed=0;
            for(int i=1;i<=s;i++) {
                for(int j=ed;j>=0;j--) {
                    if(f[j]) {
                        for(int i=1;i<=a[0];i++) {
                            f[a[i]+j]=1;
                        }
                    }
                }
                ed+=a[a[0]];
            }
            for(int i=0;i<=ed;i++) {
                if(!f[i]) {
                    ed=i-1;break;
                }
            }
            if(ed>ans||(ed==ans&&a[0]<minlen)||(ed==ans&&a[0]==minlen&&a[a[0]]<maxa)) {
                ans=ed;
                minlen=a[0];
                maxa=a[a[0]];
                memcpy(b,a,sizeof(a));
            }
        }
        printf("max coverage = %3d :",ans);
        for(int i=1;i<=b[0];i++) {
            printf(" %2d",b[i]);
        }
        puts("");
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/plysc/p/10885932.html