牛客网暑期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;
}

猜你喜欢

转载自blog.csdn.net/l18339702017/article/details/81238583