zoukankan      html  css  js  c++  java
  • 牛客多校3 A-PACM Team(状压降维+路径背包)

    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 p
    i
    , a
    i
    , c
    i
    , m
    i
    , g
    i
     indicating that i-th team consists of p
    i
     physics experts, a
    i
     algorithm experts, c
    i
     coding experts, m
    i
     math experts, and will bring g
    i
     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 ≤ p
    i
    ,a
    i
    ,c
    i
    ,m
    i
    ,g
    i
     ≤ 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


    这题的b数组处理各种卡。。int五维爆内存,想用pair存map随用随开爆时间,然后就考虑降维,将标记数组b[n][VA][VB][VC][VD]=1的第一维下标表示在b元素中
    b[VA][VB][VC][VD]=1ll<<(n-1),利用状压思想。注意因为2^36是long long级别的,所以1(一)的后面有一个ll(LL)常量类型转换。

    赛后发现这道题五维时用bool或short就可以过。。而int是27wk(比赛限制26wk)印象中第一次被卡了内存囧

    为此重温一下内存计算(64位):bool 1字节 short 2字节 int 4字节 long 8字节,1字节(B)=8位(bit),1024B=1k


    #include <bits/stdc++.h>
    
    using namespace std;
    
    typedef long long ll;
    const int MAX = 37;
    const int INF = 0x3f3f3f3f;
    
    int dp[MAX][MAX][MAX][MAX];
    int va[MAX],vb[MAX],vc[MAX],vd[MAX],w[MAX];
    ll b[MAX][MAX][MAX][MAX];
    
    int main(void)
    {
        int n,i,j,k,l,m;
        int VA,VB,VC,VD;
        scanf("%d",&n);
        for(i=1;i<=n;i++){
            scanf("%d%d%d%d%d",&va[i],&vb[i],&vc[i],&vd[i],&w[i]);
        }
        scanf("%d%d%d%d",&VA,&VB,&VC,&VD);
        for(i=1;i<=n;i++){
            for(j=VA;j>=va[i];j--){
                for(k=VB;k>=vb[i];k--){
                    for(l=VC;l>=vc[i];l--){
                        for(m=VD;m>=vd[i];m--){
                            if(dp[j][k][l][m]<=dp[j-va[i]][k-vb[i]][l-vc[i]][m-vd[i]]+w[i]){
                                dp[j][k][l][m]=dp[j-va[i]][k-vb[i]][l-vc[i]][m-vd[i]]+w[i];
                                b[j][k][l][m]|=1ll<<(i-1);
                            }
                        }
                    }
                }
            }
        }
        queue<int> q;
        i=n;
        while(i>0&&VA>=0&&VB>=0&&VC>=0&&VD>=0){
            if(b[VA][VB][VC][VD]&(1ll<<(i-1))){
                q.push(i-1);
                VA-=va[i];
                VB-=vb[i];
                VC-=vc[i];
                VD-=vd[i];
            }
            i--;
        }
        printf("%d
    ",q.size());
        int f=0;
        while(q.size()){
            if(f==0) f=1;
            else printf(" ");
            printf("%d",q.front());
            q.pop();
        }
        printf("
    ");
        //printf("%d
    ",dp[VA][VB][VC][VD]);
        return 0;
    }
    
    /*
    4
    2 1 7 4 2
    1 0 1 1 3
    2 4 5 3 28
    0 1 1 1 2
    4 1 3 5
    */
  • 相关阅读:
    [Ramda] allPass, propEq
    [Elm] Installing and setting up Elm
    [Node.js] Use nodejs-dashboard event loop delay with hrtime()
    [Node.js] Use Realm Object Database with Node.js
    [CSS] Manipulate Images Using CSS Filter and Blend Modes
    Android实现弹出输入法时,顶部固定,中间部分上移的效果
    [置顶] linux下让php支持mysql——寻找消失的mysql
    Conversion between json and object using SBJson lib
    Linux2.6内核--中断线被关闭的情况
    字符串、十六进制、byte数组互转
  • 原文地址:https://www.cnblogs.com/yzm10/p/9374264.html
Copyright © 2011-2022 走看看