牛客网暑期ACM多校训练营(第三场) PACM Team (01背包的多条件限制)

链接:https://www.nowcoder.com/acm/contest/141/A
来源:牛客网
 

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
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define clr(a) memset(a,0,sizeof(a))


const int MAXN = 1e5+10;
const int INF = 0x3f3f3f3f;
const int N = 37;

int n,A,B,C,D;
short dp[N][N][N][N][N];
bool vis[N][N][N][N][N];
struct node{
    int a,b,c,d,val;
}p[N];

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d%d%d%d%d",&p[i].a,&p[i].b,&p[i].c,&p[i].d,&p[i].val);
    scanf("%d%d%d%d",&A,&B,&C,&D);
    dp[0][0][0][0][0] = 0;

    for(int i=1;i<=n;i++){
        for(int j=0;j<=A;j++){
            for(int k=0;k<=B;k++){
                for(int l=0;l<=C;l++){
                    for(int s=0;s<=D;s++){
                        dp[i][j][k][l][s] = dp[i-1][j][k][l][s];
                        if(j>=p[i].a&&k>=p[i].b&&l>=p[i].c&&s>=p[i].d){
                            if((dp[i-1][j-p[i].a][k-p[i].b][l-p[i].c][s-p[i].d] + p[i].val) > dp[i-1][j][k][l][s]){
                                dp[i][j][k][l][s] = dp[i-1][j-p[i].a][k-p[i].b][l-p[i].c][s-p[i].d]+p[i].val;
                                vis[i][j][k][l][s] = true;
                            }
                        }
                    }
                }
            }
        }
    }
    stack<int>st;
    for(int i=n;i>=1;i--){
        if(vis[i][A][B][C][D]){
            st.push(i-1);
            A-=p[i].a;B-=p[i].b;
            C-=p[i].c;D-=p[i].d;
        }
        if(A<0||B<0||C<0||D<0)  break;
    }
    int num = st.size() ;
    int temp = 0;
    cout<<num<<endl;
    while(!st.empty()){
        if(temp != num-1){
            temp ++;
            cout<<st.top()<<" ";
            st.pop();
        }
        else {
            cout<<st.top()<<endl;
            st.pop();
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值