牛客网暑期ACM多校训练营(第三场)A.PACM Team(多重01背包)
链接: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.
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.
示例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
题意按照我的理解就是相当于一个具有多个重量W的01背包。就是从一个W扩展到P,A,C,M四个属性。
大概做法就是按照01背包的样式进行改写就可以了,因为范围很小只有36,所以时间复杂度就是个五层的循环,大概O(pow(36,5)) = 6000w左右,不会超时,这里要降一层数组,
就是4层的一个dp数组就够了。也不会超内存.使用记录路径的方法,直到最后再还原回去把加入的物品记录下来最后再输出。也就是一个很裸的做法。
#include <bits/stdc++.h> using namespace std; const int m=40; struct node{ int P,A,C,M,G; }a[m]; int A,B,C,D; int N; int dp[m][m][m][m]; bool path[m][m][m][m][m]={0};//用int超内存了 int main() { scanf("%d",&N); for(int i=0;i<N;i++){ scanf("%d%d%d%d%d",&a[i].P,&a[i].A,&a[i].C,&a[i].M,&a[i].G); } scanf("%d%d%d%d",&A,&B,&C,&D); for(int i=0;i<N;i++){ for(int j=A;j>=a[i].P;j--){ for(int k=B;k>=a[i].A;k--){ for(int l=C;l>=a[i].C;l--){ for(int o=D;o>=a[i].M;o--){ if(dp[j][k][l][o]<dp[j-a[i].P][k-a[i].A][l-a[i].C][o-a[i].M]+a[i].G){ dp[j][k][l][o]=dp[j-a[i].P][k-a[i].A][l-a[i].C][o-a[i].M]+a[i].G; path[i][j][k][l][o] = 1; } } } } } } vector<int> ans; for(int i=N-1; i>=0 && A>=0 && B>=0 && C >=0 && D>=0 ;i--) { if(path[i][A][B][C][D]) { ans.push_back(i); A -= a[i].P,B -= a[i].A; C -= a[i].C,D -= a[i].M; } } int sz = ans.size(); printf("%d\n",sz); for(int i=0;i<sz;i++) { printf(i==sz-1?"%d\n":"%d ",ans[i]); } return 0; }