2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]
题目链接:https://www.nowcoder.com/acm/contest/141/A
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld
题目描述
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.
输入描述:
输出描述:
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
题意&题解:
背包有四个约束P,A,C,M(相当于四种容量),每个物品有对应的四种体积p,a,c,m,同时还有一个价值g,问选哪些物品使得不超容量的情况下价值最大。
即一个四个约束条件的01背包,适当修改一下01背包模板即可。
另外,本题卡空间复杂度,int类型的dp数组只能开四维,所以就要用滚动数组压缩,
另外本题需要知道的是选择了哪些物品,所以开一个五维的bool类型数组存储是否选取该物品即可(365B ≈ 60000 KB,不会超空间限制)。
AC代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=37; int n; int p[maxn],a[maxn],c[maxn],m[maxn],g[maxn]; int P,A,C,M; int dp[maxn][maxn][maxn][maxn]; bool pick[maxn][maxn][maxn][maxn][maxn]; vector<int> ans; int main() { cin>>n; for(int i=0;i<n;i++) cin>>p[i]>>a[i]>>c[i]>>m[i]>>g[i]; cin>>P>>A>>C>>M; for(int i=1;i<n;i++) { for(int pp=P;pp>=0;pp--) { for(int aa=A;aa>=0;aa--) { for(int cc=C;cc>=0;cc--) { for(int mm=M;mm>=0;mm--) { if(pp<p[i]||aa<a[i]||cc<c[i]||mm<m[i]) { dp[pp][aa][cc][mm] = dp[pp][aa][cc][mm]; pick[i][pp][aa][cc][mm] = 0; } else { if(dp[pp][aa][cc][mm] < dp[pp-p[i]][aa-a[i]][cc-c[i]][mm-m[i]]+g[i]) { dp[pp][aa][cc][mm] = dp[pp-p[i]][aa-a[i]][cc-c[i]][mm-m[i]] + g[i]; pick[i][pp][aa][cc][mm] = 1; } else { dp[pp][aa][cc][mm] = dp[pp][aa][cc][mm]; pick[i][pp][aa][cc][mm] = 0; } } } } } } } ans.clear(); for(int i=n-1;i>=0;i--) { if(pick[i][P][A][C][M]) { ans.push_back(i); P-=p[i], A-=a[i], C-=c[i], M-=m[i]; } if(P<0||A<0||C<0||M<0) break; } cout<<ans.size()<<endl; for(int i=0;i<ans.size();i++) { if(i!=0) printf(" "); printf("%d",ans[i]); } }
注:用滚动数组压缩时要记得要逆序枚举容量,当然本题不逆序也可以过(因为我忘记逆序枚举容量交了一发过了),但是保持严谨性还是逆序枚举比较好。
转载请注明出处:https://dilthey.cnblogs.com/