PACM Team (01背包,卡空间)

本文介绍了一种基于01背包问题的算法,用于在物理、算法、编程和数学专家中选择最优组合,以组建一支强大的ACM竞赛队伍。通过状态压缩和动态规划,确保团队在不超过各领域专家数量限制的情况下获得最高的知识积分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:https://www.nowcoder.com/acm/contest/141/A
来源:牛客网
 

Eddy was a contestant participating in ACM ICPC contests. ACM is short for Algorithm, Coding, Math. Since in the ACM contest, the most important knowledge is about algorithm, followed by coding(implementation ability), then math. However, in the ACM ICPC World Finals 2018, Eddy failed to solve a physics equation, which pushed him away from a potential medal.

Since then on, Eddy found that physics is actually the most important thing in the contest. Thus, he wants to form a team to guide the following contestants to conquer the PACM contests(PACM is short for Physics, Algorithm, Coding, Math).

There are N candidate groups each composed of pi physics experts, ai algorithm experts, ci coding experts, mi math experts. For each group, Eddy can either invite all of them or none of them. If i-th team is invited, they will bring gi knowledge points which is calculated by Eddy's magic formula. Eddy believes that the higher the total knowledge points is, the better a team could place in a contest. But, Eddy doesn't want too many experts in the same area in the invited groups. Thus, the number of invited physics experts should not exceed P, and A for algorithm experts, C for coding experts, M for math experts.

Eddy is still busy in studying Physics. You come to help him to figure out which groups should be invited such that they doesn't exceed the constraint and will bring the most knowledge points in total.

输入描述:

The first line contains a positive integer N indicating the number of candidate groups.
Each of following N lines contains five space-separated integer pi, ai, ci, mi, gi indicating that i-th team consists of pi physics experts, ai algorithm experts, ci coding experts, mi math experts, and will bring gi knowledge points.
The last line contains four space-separated integer P, A, C, M indicating the maximum possible number of physics experts, algorithm experts, coding experts, and math experts, respectively.

 1 ≤ N ≤ 36
 0 ≤ pi,ai,ci,mi,gi ≤ 36
 0 ≤ P, A, C, M ≤ 36

输出描述:

The first line should contain a non-negative integer K indicating the number of invited groups.
The second line should contain K space-separated integer indicating the index of invited groups(groups are indexed from 0).

You can output index in any order as long as each index appears at most once. If there are multiple way to reach the most total knowledge points, you can output any one of them. If none of the groups will be invited, you could either output one line or output a blank line in the second line.

示例1

输入

复制

2
1 0 2 1 10
1 0 2 1 21
1 0 2 1

输出

复制

1
1

示例2

输入

复制

1
2 1 1 0 31
1 0 2 1

输出

复制

0
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)

const int maxn=40;

struct node{
    int sum;
    ll used;
    node(int _sum=0,ll _used=0){
        sum=_sum,used=_used;
    }
}dp[maxn][maxn][maxn][maxn];

struct S{
    int p,a,c,m,g;
    S(int _p=0,int _a=0,int _c=0,int _m=0,int _g=0){
        p=_p,a=_a,c=_c,m=_m,g=_g;
    }
}s[maxn];

int arr[maxn];
ll y[maxn];
void init(){
    y[1]=2;
    rep(i,2,36+1)   y[i]=y[i-1]<<1;
}
/*
01背包问题,逆序枚举容量就好了

难点:空间复杂度:
做法:
1.滚动数组
2.状态压缩到(long long)类型的数字中

坑点:
不知道为什么(ll)(1<<i),为什么不对,提前预处理一下就好了
*/
int main(){
    init();
    int n;
    scanf("%d",&n);
    rep(i,1,n+1){
        int p,a,c,m,g;
        scanf("%d %d %d %d %d",&p,&a,&c,&m,&g);
        s[i]=S(p,a,c,m,g);
    }
    int P,A,C,M;
    scanf("%d %d %d %d",&P,&A,&C,&M);
    rep(i,1,n+1){
        per(p,s[i].p,P+1){
            per(a,s[i].a,A+1){
                per(c,s[i].c,C+1){
                    per(m,s[i].m,M+1){
                        node& now=dp[p][a][c][m];
                        node& pre=dp[p-s[i].p][a-s[i].a][c-s[i].c][m-s[i].m];
                        if(now.sum<pre.sum+s[i].g){
                            now.sum=pre.sum+s[i].g;
                            now.used=(pre.used|y[i]);
                        }
                    }
                }
            }
        }
    }

    ll tmp=dp[P][A][C][M].used;
    int cnt=0;
    rep(i,1,36+1){
        if(tmp&y[i]){
            arr[cnt++]=i-1;
        }
    }

    printf("%d\n",cnt);
    rep(i,0,cnt){
      if(i) printf(" %d",arr[i]);
      else printf("%d",arr[i]);
    }
    printf("\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值