PACM Team

链接: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.

示例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

大意;

要招 Physics, Algorithm, Coding, Math老师,又若干个团队,每个团队都这四种老师的相应人数,还有一个你如果股这个团队会获得的价值,当然顾每个团队的时候,这四种老师需求的数量不能小于这个团队每种老师的数量,问怎么股团队可以获得最大的价值

题解;

01背包

记录路径的时候记得开bool类型可以开5维的path[38][38][38][38][38]:

最后找的时候每个团队的编号从后往前找,因为for循环里是从0开始的,如果后面的路径被记录下来了肯定是更优的解法,否则不会进if里,也不会替换前面的

代码:

#include<bits/stdc++.h>
using namespace std;
const int MAX = 38;
int dp[MAX][MAX][MAX][MAX];
bool path[MAX][MAX][MAX][MAX][MAX];
int p[MAX],a[MAX],c[MAX],m[MAX], g[MAX],cnt[MAX];
int N,P,A,C,M;
void solve(){
     memset(dp,0,sizeof(dp));
    memset(path,0,sizeof(path));
    for(int i=0;i<N;++i){
        for(int j=P;j >= p[i];--j){
            for(int k=A;k >= a[i];--k){
                for(int l=C;l >= c[i];--l){
                    for(int o=M;o >= m[i];--o){
                        if(dp[j][k][l][o] < dp[j-p[i]][k-a[i]][l-c[i]][o-m[i]] + g[i]){
                            dp[j][k][l][o] = dp[j-p[i]][k-a[i]][l-c[i]][o-m[i]] + g[i];
                            path[i][j][k][l][o] = true;//如果被更新就标记这个情况为true
                            
                        }
                    }
                }
            }
        }
    }
    vector<int> vec;
    for(int i=N-1,j=P,k=A,l=C,o=M;i>=0&&j>=0&&k>=0&&l>=0&&o>=0;--i){
        if(path[i][j][k][l][o]){//表示这个物品被使用了。
            vec.push_back(i);
            j -= p[i];
            k -= a[i];
            l -= c[i];
            o -= m[i];
        }
    }
    cout << vec.size() << endl;
    for(vector<int>::iterator it = vec.begin();it != vec.end();++it){
        cout << *it << endl;
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    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;
    solve();
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值