题目链接: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. dos命令收集

    应用程序和进程相关 tasklist  /svc     查看计算机当前正在运行程序 taskkill /f /im "eclipse.exe"   关闭指定的应用程序 taskk ...

  2. hbase2.0.0-安装部署

    依赖hadoop 环境,我这边的版本是hadoop-2.6.5 选择hbase2.0.0版本的时候,去官网查看支持的hadoop版本 1.伪分布式安装 下载:http://mirror.bit.edu ...

  3. springboot测试service层的单元测试

    package com.test.service; import com.task.Application;import com.task.model.po.TaskRecordDo;import o ...

  4. Cesium加载影像和地形数据+开启高程遮挡效果+视点定位+定时更新

    // 初始化Cesium var viewer = new Cesium.Viewer('cesiumContainer', { /*imageryProvider : new Cesium.ArcG ...

  5. ASP.NET MVC入门到精通——数据库仓储

    业务层调用数据层对象,我不想每次都new一个数据层对象,而是在数据层创建一个仓储,统一管理所有的对象调用. 1.在IDAL项目中,新建IDBSession.tt模板   Ctrl+S后自动生成IDBS ...

  6. Servlet基本用法(一)基本配置

    一.前言 Java Servlet是一个基于Java技术的Web组件,运行在服务器端,由Servlet容器所管理,用于生成动态的内容.Servlet是平台独立的Java类,编写一个Servlet实际上 ...

  7. Android数据存储之SD卡

    为了更好的存取应用程序的大文件数据,应用程序需要读. 写SD卡上的文件.SD卡大大扩充手机的存储能力. 操作SD首先要加权限: <!--在SDCard中创建与删除文件权限 --> < ...

  8. cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration的解决

    导入了一个工程,编译什么的都还好,但是报了一个XML的错误. cvc-complex-type.2.4.c: The matching wildcard is strict, but no decla ...

  9. JDBC批量执行executeBatch

    JDBC事务 在数据库中,所谓事务是指一组逻辑操作单元,使数据从一种状态变换到另一种状态.为确保数据库中数据的一致性,数据的操纵应当是离散的成组的逻辑单元:当它全部完成时,数据的一致性可以保持,而当这 ...

  10. Linux IPC BSD socket编程基础

    头文件 #include<unistd.h> #include <sys/types.h> #include <sys/socket.h> #include< ...