zoukankan      html  css  js  c++  java
  • 牛客多校第三场 A—pacm team (4维背包加路径压缩)

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

    一开始写了五维,内存超限了

    36我们发现2的36次不会超ll

    所以用状压去存路径

    #include <iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    #define ll long long
    struct node
    {
        int a,b,c,d,v;
    }p[37];
    int dp[37][37][37][37];
    ll path[37][37][37][37];
    int main()
    {
        int n;
        scanf("%d",&n);
        memset(dp,0,sizeof(dp));
        memset(path,0,sizeof(path));
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d%d%d",&p[i].a,&p[i].b,&p[i].c,&p[i].d,&p[i].v);
        }
        int a,b,c,d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        for(int i=0;i<n;i++)
        {
            for(int j=a;j>=0;j--)
                for(int k=b;k>=0;k--)
                    for(int q=c;q>=0;q--)
                        for(int w=d;w>=0;w--)
                        {
                            if(p[i].a>j||p[i].b>k||p[i].c>q||p[i].d>w)
                                continue;
                            else if(dp[j-p[i].a][k-p[i].b][q-p[i].c][w-p[i].d]+p[i].v>dp[j][k][q][w])
                            {
                                dp[j][k][q][w]=dp[j-p[i].a][k-p[i].b][q-p[i].c][w-p[i].d]+p[i].v;
                                path[j][k][q][w]=path[j-p[i].a][k-p[i].b][q-p[i].c][w-p[i].d]|(1ll<<i);
                            }
                        }
        }
        //cout<<dp[a][b][c][d]<<endl;
        //cout<<path[a][b][c][d]<<endl;
        int f=0;
        int p[1000];
            for(int i=0;i<n;i++)
            {
                if(path[a][b][c][d]&(1ll<<i))
                {
                    p[f++]=i;
                }
            }
        cout<<f<<endl;
        for(int i=0;i<f;i++)
        {
            if(i)printf(" ");
            printf("%d",p[i]);
        }
        printf("
    ");
        
        return 0;
    }
    
  • 相关阅读:
    数组中最大和的子数组
    数据结构与算法面试题80道
    fcntl获取和修改文件打开状态标志
    dup等复制文件描述符函数
    截断文件函数truncate和ftruncate
    浅析
    五个Taurus垃圾回收compactor优化方案,减少系统资源占用
    如何用交互式特征工程工具进行数据分析处理
    【华为云技术分享】解密如何使用昇腾AI计算解决方案构建业务引擎
    Scrum Master教你四招,瓦解团队内部刺头
  • 原文地址:https://www.cnblogs.com/2014slx/p/9375940.html
Copyright © 2011-2022 走看看