PACM TEAM(小剪枝)

探讨在ACM竞赛中如何合理选择不同技能背景的成员来组建最强团队,通过具体示例和代码实现,介绍了一种有效的团队配置算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

感受:开始以为要用dp。后来实验室里其他人说暴力能过。于是就开始敲。然后发现自己一个地方写挫了。

代码中注释的地方就是我错误的地方。当时居然没有看出来~

还要有个小剪枝。用sum[i]记录在包括i和i后边的所有的和。然后判断一下如果后边都取,和ans进行比较大小。比ans小的话,直接return就好了。

代码:

#include<bits/stdc++.h>
using namespace std;
int n;
int A,B,C,D;
int ans;
struct node
{
    int a,b,c,d,val;
    int id;
}lala[40];
int vis[40],viss[40];
int sum[40];

void dfs(int aa,int bb,int cc,int dd,int id,int money)
{
    ///cout<<"id:"<<id<<endl;
    if(id==n)
    {
       /// cout<<"ans"<<ans<<endl;
        if(ans<money)
        {
            ans=max(ans,money);
            for(int i=0;i<=n-1;i++)
            {
                if(vis[i]==1)
                    viss[i]=1;
                else
                    viss[i]=0;
            }
        }
      ///  cout<<"^^^"<<endl;
        return ;
    }
    if(money+sum[id]<ans)
    {
       /// cout<<"ppp"<<endl;
        return ;
    }
    vis[id]=1;

    int a1=lala[id].a;
    int b1=lala[id].b;
    int c1=lala[id].c;
    int d1=lala[id].d;

    /*
    if(aa+a1>A||bb+b1>B || cc+c1>C ||dd+d1>D)
    {
        if(ans<money)
        {
            ans=max(ans,money);
            for(int i=0;i<=n-1;i++)
            {
                if(vis[i]==1)
                    viss[i]=1;
                else
                    viss[i]=0;
            }
        }
        cout<<"***"<<endl;
        return;
    }
    */
    if(aa+a1<=A && bb+b1<=B && cc+c1<=C &&dd+d1<=D)
        dfs(aa+lala[id].a,bb+lala[id].b,cc+lala[id].c,dd+lala[id].d,id+1,money+lala[id].val);
    vis[id]=0;
    dfs(aa,bb,cc,dd,id+1,money);

}
int main()
{
    memset(vis,0,sizeof(vis));
    memset(viss,0,sizeof(viss));
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d%d%d%d",&lala[i].a,&lala[i].b,&lala[i].c,&lala[i].d,&lala[i].val);
        lala[i].id=i-1;
    }

    sum[n-1]=lala[n-1].val;

    for(int i=n-2;i>=0;i--)
    {
        sum[i]=lala[i].val+sum[i+1];///就是说选了这个之后所获得的最大价值(包含这个数)
    }
    scanf("%d%d%d%d",&A,&B,&C,&D);

    ans=0;
    dfs(0,0,0,0,0,0);
    int kaka=0;
    for(int i=0;i<=35;i++)
    {
        if(viss[i]==1)
            kaka++;
    }
    printf("%d\n",kaka);
    for(int i=0;i<=35;i++)
    {
        if(viss[i]==1)
           printf("%d\n",i);
    }

   /// cout<<ans<<endl;

}
/*
2
1 0 2 1 10
1 0 2 1 21
1 0 2 1

10
0 0 0 0 2
0 0 0 0 3
0 0 0 0 4
0 0 0 0 5
0 0 0 0 6
0 0 0 0 7
0 0 0 0 8
0 0 0 0 9
0 0 0 0 10
0 0 0 0 12
0 0 0 0


3
0 0 0 0 4
9 9 9 9 9
8 8 8 8 8
0 0 0 0



3
9 9 9 9 9
8 8 8 8 8
0 0 0 0 10
0 0 0 0

*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值