题目链接:https://www.nowcoder.com/acm/contest/141/A

时间限制:C/C++ 1秒,其他语言2秒
空间限制: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.

输入描述:

输出描述:

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=; 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=;i<n;i++) cin>>p[i]>>a[i]>>c[i]>>m[i]>>g[i];
cin>>P>>A>>C>>M; for(int i=;i<n;i++)
{
for(int pp=P;pp>=;pp--)
{
for(int aa=A;aa>=;aa--)
{
for(int cc=C;cc>=;cc--)
{
for(int mm=M;mm>=;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] = ;
}
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] = ;
}
else
{
dp[pp][aa][cc][mm] = dp[pp][aa][cc][mm];
pick[i][pp][aa][cc][mm] = ;
}
}
}
}
}
}
} ans.clear();
for(int i=n-;i>=;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<||A<||C<||M<) break;
} cout<<ans.size()<<endl;
for(int i=;i<ans.size();i++)
{
if(i!=) printf(" ");
printf("%d",ans[i]);
}
}

注:用滚动数组压缩时要记得要逆序枚举容量,当然本题不逆序也可以过(因为我忘记逆序枚举容量交了一发过了),但是保持严谨性还是逆序枚举比较好。

2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]的更多相关文章

  1. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  2. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

  3. 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)

    Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...

  4. 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)

    题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...

  5. 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)

    题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...

  6. 2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]

    题目链接:https://www.nowcoder.com/acm/contest/140/J 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K ...

  7. 2018牛客网暑期ACM多校训练营(第二场) A - run - [DP]

    题目链接:https://www.nowcoder.com/acm/contest/140/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K ...

  8. 2018牛客网暑期ACM多校训练营(第一场) D - Two Graphs - [无向图同构]

    题目链接:https://www.nowcoder.com/acm/contest/139/D 题目描述 Two undirected simple graphs  and  where  are i ...

  9. 2018牛客网暑期ACM多校训练营(第一场) J - Different Integers - [莫队算法]

    题目链接:https://www.nowcoder.com/acm/contest/139/J 题目描述  Given a sequence of integers a1, a2, ..., an a ...

  10. 2018牛客网暑期ACM多校训练营(第九场)A -Circulant Matrix(FWT)

    分析 大佬说看样例就像和卷积有关. 把题目化简成a*x=b,这是个xor的FWT. FWT的讲解请看:https://www.cnblogs.com/cjyyb/p/9065615.html 那么要求 ...

随机推荐

  1. MongoDB的数据模型

    文档的数据模型代表了数据的组织结构,一个好的数据模型能更好的支持应用程序.在MongoDB中,文档有两种数据模型,内嵌(embed)和引用(references). 内嵌 MongoDB的文档是无模式 ...

  2. 有人在群里问mysql如何选择性更新部分条件的问题

    有人在群里问这个问题 update xt_kh set zhye=zhye+1,hzyj=hzyj+1 where dlgh='kiss0451' and hzms=1 如果这样写 hzms不等于1的 ...

  3. iOS protocbuf安装使用

    protobuf文件地址:https://github.com/google/protobuf 1.问题/usr/local.bak/lib /usr/local.bak/man /usr/local ...

  4. mysql5.7 服务无法启动的问题解决方法

    解决办法: 1.把MySQL文件低下的data文件删掉,如果没有的话,就不用管了: 2.在mysql安装路径下,执行mysqld --initialize命令进行初始化,mysql会自动帮你重新创建d ...

  5. 【RF库XML测试】测试的XML文件说明

    文件存放路径:C:\workspace\robotframework\test_rf_api\testdata\XML.xml 文件内容: <example> <first id=& ...

  6. Git 学习笔记--1.Git基础操作

    取得项目的Git仓库 有两种方式取得Git项目仓库.第一种是在现存的目录下,通过导入所有文件来创建新的Git仓库.第二种是从已有的Git仓库克隆出一个新的镜像仓库. 在工作目录中初始化新仓库  要对现 ...

  7. pip导出安装包及批量安装

    python导出安装包及版本 pip freeze > requirements.txt 批量安装pip install -r requirements.txt

  8. 嵌入式ROOTFS transplantation

    作一个嵌入式Linux rootfs,并且实现 web 服务 1. 文件系统简介 •理论上说一个嵌入式设备如果内核能够运行起来,且不需要运行用户进程的话,是不需要文件系统的,文件系统简单的说就是一种目 ...

  9. Xcode 7.3 cannot create __weak reference in file using manual reference counting

      原帖地址 http://stackoverflow.com/questions/36147625/xcode-7-3-cannot-create-weak-reference-in-file-us ...

  10. 2. React组件的生命周期

    2. React组件的生命周期 使用React开发时候用到最多的就是React的组件了,通过继承React.Component,加入constructor构造函数,实现Render方法即可.这当中Re ...