(第三场) A PACM Team 【dp,五维背包】
链接:https://www.nowcoder.com/acm/contest/141/A
来源:牛客网
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld
题目描述
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 p i, 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.
case1:
输入
2
1 0 2 1 10
1 0 2 1 21
1 0 2 1
输出
1
1
case2:
输入
1 2 1 1 0 31
1 0 2 1
输出
0
题目大意:有一个四维背包,求取得最大值的方式(即有多少组,和具体组的 index)
思路:背包,但是要注意数据的类型要用short或者char,否则会爆内存。其次不能用滚动数组优化,因为要回溯到具体哪一组。做法一就是单纯的五维背包
状态:dp[ i ][ p ][ a ][ c ][ m ] 走了 i 组背包容量为 p, a, c, m 的最大值
状态转移: dp[ i ][ p ][ a ][ c ][ m ] = max(dp[i-1][ p ][ a ][ c ][ m ], dp[i-1][ p-ip[i] ][ a-ia[i] ][ c-ic[i] ][ m-im[i] ]);
这里回溯答案的话,用一个bool in[ i ][ p ][ a ][ c ][ m ]记录在当前状态有没有取第 i 组;
AC code:
- #include <vector>
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- #define INF 0x3f3f3f3f
- using namespace std;
- const int MAXN = ;
- short dp[MAXN][MAXN][MAXN][MAXN][MAXN];
- int ip[MAXN], ia[MAXN], ic[MAXN], im[MAXN], g[MAXN];
- bool in[MAXN][MAXN][MAXN][MAXN][MAXN];
- int N, P, A, C, M;
- void slv()
- {
- for(int i = ; i <= N; i++) ///初始化
- for(int p = ; p <= P; p++)
- for(int a = ; a <= A; a++)
- for(int c = ; c <= C; c++)
- for(int m = ; m <= M; m++)
- in[i][p][a][c][m] = false;
- for(int i = ; i <= N; i++)
- {
- for(int p = ; p <= P; p++) ///省去下面比较前一个取或者不取,一律初始化为不取
- for(int a = ; a <= A; a++)
- for(int c = ; c <= C; c++)
- for(int m = ; m <= M; m++)
- {
- dp[i][p][a][c][m] = dp[i-][p][a][c][m];
- }
- for(int p = P; p >= ip[i]; p--)
- for(int a = A; a >= ia[i]; a--)
- for(int c = C; c >= ic[i]; c--)
- for(int m = M; m >= im[i]; m--)
- {
- if(dp[i][p][a][c][m] < (dp[i-][p-ip[i]][a-ia[i]][c-ic[i]][m-im[i]]+g[i]))
- {
- dp[i][p][a][c][m] = dp[i-][p-ip[i]][a-ia[i]][c-ic[i]][m-im[i]]+g[i];
- in[i][p][a][c][m] = true; ///取了第 i 组
- }
- }
- }
- vector<int>ans;
- for(int i = N; i > ; i--)
- {
- if(in[i][P][A][C][M])
- {
- ans.push_back(i-); ///题目下标从 0 开始
- P-=ip[i]; ///但是这里数组下标是从 1 开始
- A-=ia[i];
- C-=ic[i];
- M-=im[i];
- }
- }
- printf("%d\n", (int)ans.size());
- for(size_t i = ; i < ans.size(); i++)
- {
- printf("%d", ans[i]);
- if(i+ < ans.size()) printf(" ");
- else printf("\n");
- }
- }
- int main()
- {
- scanf("%d", &N);
- for(int i = ; i <= N; i++)
- {
- scanf("%d %d %d %d %d", &ip[i], &ia[i], &ic[i], &im[i], &g[i]);
- }
- scanf("%d %d %d %d", &P, &A, &C, &M);
- slv();
- return ;
- }
(第三场) A PACM Team 【dp,五维背包】的更多相关文章
- 牛客多校第三场 A—pacm team (4维背包加路径压缩)
链接:https://www.nowcoder.com/acm/contest/141/A 来源:牛客网 Eddy was a contestant participating , Eddy fail ...
- 牛客网多校训练第三场 A - PACM Team(01背包变形 + 记录方案)
链接: https://www.nowcoder.com/acm/contest/141/A 题意: 有n(1≤n≤36)个物品,每个物品有四种代价pi,ai,ci,mi,价值为gi(0≤pi,ai, ...
- PACM Team(牛客第三场多校赛+dp+卡内存+打印路径)
题目链接(貌似未报名的不能进去):https://www.nowcoder.com/acm/contest/141/A 题目: 题意:背包题意,并打印路径. 思路:正常背包思路,不过五维的dp很容易爆 ...
- dp - 3维背包(东四省)
题意: 给你 n 张卡片,总共可以消耗的法力值,求最多可以造成多少伤害, 卡片分为2种,一种是魔法卡,使用后可以使所有的连环卡的费用全部减1,另一种是连环卡,因魔法卡的使用可以使其费用减1,问最终最多 ...
- 2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]
题目链接:https://www.nowcoder.com/acm/contest/141/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 牛客多校第三场 A- PACM Team 背包/记忆路径
https://www.nowcoder.com/acm/contest/141#question 一眼背包,用四维dp记录在A,B,C,D条件限制下可以获得的最大知识点,但是题目要求输出路径,在输入 ...
- 2018 HDU多校第三场赛后补题
2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...
- BZOJ_1864_[Zjoi2006]三色二叉树_树形DP
BZOJ_1864_[Zjoi2006]三色二叉树_树形DP 题意: 分析:递归建树,然后DP,从子节点转移. 注意到红色和蓝色没有区别,因为我们可以将红蓝互换而方案是相同的.这样的话我们只需要知道当 ...
- 2018牛客网暑假ACM多校训练赛(第三场)I Expected Size of Random Convex Hull 计算几何,凸包,其他
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-I.html 题目传送门 - 2018牛客多校赛第三场 I ...
随机推荐
- unity烘焙记录
1.Unity Android 阴影不显示.阴影显示不正确 解决 http://blog.csdn.net/xuetao0605/article/details/50626181 2.阴影强度问题 不 ...
- pat00-自测3. 数组元素循环右移问题 (20)
00-自测3. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在 ...
- 深入理解JavaScript系列(31):设计模式之代理模式
介绍 代理,顾名思义就是帮助别人做事,GoF对代理模式的定义如下: 代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问. 代理模式使得代理对象控制具体对象的引用.代理几乎可以是任何对 ...
- Sturct类型装箱时会遇到的问题
Object在拆箱时会在栈空间生成一个临时变量.所以Struct在使用时尽量将内容都声明为readonly为好 [<Struct>] type Point= val mutable X:d ...
- node express formidable 文件上传后修改文件名
//我是用php的思想来学习nodejs var express = require('express'); var router = express.Router(); var fs = requi ...
- [LeetCode]29. Divide Two Integers两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- mysql三-1:理解存储引擎
一.什么是存储引擎 mysql中建立的库===>文件夹 库中建立的表===>文件 生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不同的处理机制:比如处理文本用txt类型,处 ...
- github使用手册
1.git init 2.git add README.md (增加文件夹/文件:git add dir/files) 3.git commit -m "注释内容” 4.git push - ...
- CSS3媒体查询总结
1.什么是媒体查询 媒体查询可以让我们根据设备显示器的特性(如视口宽度.屏幕比例.设备方向:横向或纵向)为其设定CSS样式,媒体查询由媒体类型和一个或多个检测媒体特性的条件表达式组成.媒体查询中可用于 ...
- Stage2--Python的数据类型
说在前面: Stage1-Stage4简单介绍一下Python语法,Stage5开始用python实现一些实际应用,语法的东西到处可以查看到,学习一门程序语言的最终目的是应用,而不是学习语法,语法本事 ...