牛客网暑期ACM多校训练营(第三场)A.PACM Team 多维01背包+路径记录

3 篇文章 0 订阅
1 篇文章 0 订阅

链接: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 contains a positive integer N indicating the number of candidate groups.
Each of following N lines contains five space-separated integer pi, 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.

 

输入

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

题目大意:

Eddy现在想招募多支队伍辅助他,每一个队伍有4种职务,分配是p,a,c,m和1个权值g。

现在Eddy想在招募队伍各个职务的人数各不超过P,A,C,M的条件下使得g的和最大。

输出选择的队伍的编号(编号从0开始),如果有相同结果时任意输出。

题目分析:

显然这就是一个01背包加强版,只是这个物品是有4个质量的而已。

比较麻烦的是记录路径,这里笔者用的方法是通过记录某一个状态下取了那一个物品,然后通过这个物品回溯到前一个状态。具体操作看代码,这里有一点要注意的是,记录状态的数组因为有5维,因此必须使用小内存的变量类型(例如bool和char),int已经确定会爆内存的。

#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<set>
#include<cstdio>
#include<functional>
#include<iomanip>
#include<cmath>
#include<stack>
#include<iomanip>
#include<functional>
#include<iomanip>
#include<bitset>
#define lson l,m
#define rson m+1,r
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
const int maxn = 5 * int(1e5) + 100;
const int BN = 30;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = (int)1e9 + 7;
const double eps = 1e-6;
int dp[40][40][40][40];
bool path[40][40][40][40][40];
struct nodes {
    int p, a, c, m, g;
}num[40], lim;
stack<int>ans;
void solve(int i, int j, int k, int l, int o) {
    while (i && (j || k || l || o)) {
        if (path[i][j][k][l][o]) {
            ans.push(i - 1);
            j -= num[i].p, k -= num[i].a;
            l -= num[i].c, o -= num[i].m;
        }
        i--;
    }
}
int main() {
    //freopen("test.in","r",stdin);
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d%d%d%d%d", &num[i].p, &num[i].a, &num[i].c, &num[i].m, &num[i].g);
    memset(dp, 0, sizeof(dp));
    memset(path, 0, sizeof(path));
    while (!ans.empty()) ans.pop();
    scanf("%d%d%d%d", &lim.p, &lim.a, &lim.c, &lim.m);
    for (int i = 1; i <= n; i++) {
        for (int j = lim.p; j >= num[i].p; j--) {
            for (int k = lim.a; k >= num[i].a; k--) {
                for (int l = lim.c; l >= num[i].c; l--) {
                    for (int o = lim.m; o >= num[i].m; o--) {
                        int k1 = dp[j][k][l][o], k2 = dp[j - num[i].p][k - num[i].a][l - num[i].c][o - num[i].m] + num[i].g;
                        if (k1<k2) {
                            dp[j][k][l][o] = k2;
                            path[i][j][k][l][o] = 1;
                        }
                    }
                }
            }
        }
    }
    solve(n, lim.p, lim.a, lim.c, lim.m);
    int len = ans.size();
    printf("%d\n", len);
    while (!ans.empty()) {
        printf("%d", ans.top());
        ans.pop();
        printf("%s", !ans.empty() ? " " : "");
    }
    printf("\n");
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值