牛客多校第三场 A PACM Team(背包+记录路径)

链接: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

题目大意:现在一共有N个专家团队,每个专家团队里都有p[i]个物理专家,a[i]个算法专家,c[i]个代码专家,m[i]个数学专家,能为你提供g[i]点知识点。你每次都只能选择一个团队里的所有专家,但是你不能选择超过P个物理专家,A个算法专家,C个代码专家,M个数学专家。问你要如何选择团队,才能使得你能获得的知识点达到最大,输出你所选择的团队。

题目思路:这种题显然就是一个01背包的题目,只是将背包的容量上升为4维。

我们以dp[i][j][k][l][o]表示前 i 个专家团队里选择了 j 个物理专家,k 个算法专家,l 个代码专家,o 个数学专家所能获得的最大知识点。

很显然可以推出如下的状态转移方程

dp[i][j][k][l][o]=max(dp[i-1][j][k][l][o],dp[i-1][j-p[i]][k-a[i]][l-c[i]][o-m[i]])

由于本题是要记录路径,所以可以再开一个vis[i][j][k][l][o]来记录在这个状态时是否选择了第 i 个专家团队,最后再从结果跑回最初的状态。

但这题有一个坑点,由于要开两个36^5的数组,内存是扛不住的。。。比赛时成功MLE两发。

解决方法的话,可以把dp数组设为short,vis设为bool可以卡过去,更好的方法就是用滚动数组的方法,把dp数组的第一维滚动掉,就可以节省大量的内存。

具体实现看代码:

#include <bits/stdc++.h>
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) x&-x
#define MP make_pair
#define pb push_back
#define debug(x) cout<<"x= "<<x<<endl;
#define FIN freopen("in.txt","r",stdin);
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int>pii;
const int mod=1e9+7;
const ll infll=0x3f3f3f3f3f3f3f3f;
const int MX = 1e5 + 7;
const int INF = 0x3f3f3f3f;

short n;
short dp[37][37][37][37];
bool vis[37][37][37][37][37];
short p[37],a[37],c[37],m[37],g[37];
short P,A,C,M;

int main(){
    //FIN;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>p[i]>>a[i]>>c[i]>>m[i]>>g[i];
    cin>>P>>A>>C>>M;
    memset(dp,0,sizeof(dp));
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++){
        for(int j=P;j>=p[i];j--){
            for(int k=A;k>=a[i];k--){
                for(int l=C;l>=c[i];l--){
                    for(int o=M;o>=m[i];o--){
                        if(dp[j][k][l][o] > dp[j-p[i]][k-a[i]][l-c[i]][o-m[i]]+g[i]) continue;
                        dp[j][k][l][o]=dp[j-p[i]][k-a[i]][l-c[i]][o-m[i]]+g[i];
                        vis[i][j][k][l][o]=1;
                    }
                }
            }
        }
    }
    int ans[37],num=0;
    while(n){
        if(vis[n][P][A][C][M]){
            P-=p[n];
            A-=a[n];
            C-=c[n];
            M-=m[n];
            ans[++num]=n-1;
        }n--;
    }
    printf("%d\n",num);
    for(int i=num;i>=1;i--)
        printf("%d ",ans[i]);
    printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lee_w_j__/article/details/81235479
今日推荐