• 牛客网暑期ACM多校训练营(第三场) A PACM Team 01背包 记录路径


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

    题意:在满足不超过四个限制的基础上的最大值
    分析:01背包的扩展,题目要求我们记录路径,所以我们在求出最大背包容量时记录下哪些点走过,最后再遍历下就可以求出路径
    #include <map>
    #include <set>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <iomanip>
    #include <iostream>
    #include <algorithm>
    #define debug(a) cout << #a << " " << a << endl
    using namespace std;
    typedef long long ll;
    const ll maxn = 40;
    const ll mod = 1000000000000023;
    ll n, P, A, C, M;
    ll p[maxn], a[maxn], c[maxn], m[maxn], g[maxn];
    ll f[maxn][maxn][maxn][maxn];
    bool vis[maxn][maxn][maxn][maxn][maxn];
    int main() {
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        cin >> n;
        for( ll i = 1; i <= n; i ++ ) {
            cin >> p[i] >> a[i] >> c[i] >> m[i] >> g[i];
        }
        cin >> P >> A >> C >> M;
        for( ll i = 1; i <= n; i ++ ) {
            for( ll jp = P; jp >= p[i]; jp -- ) {
                for( ll ja = A; ja >= a[i]; ja -- ) {
                    for( ll jc = C; jc >= c[i]; jc -- ) {
                        for( ll jm = M; jm >= m[i]; jm -- ) {
                            if( f[jp-p[i]][ja-a[i]][jc-c[i]][jm-m[i]] + g[i] > f[jp][ja][jc][jm] ) {
                                f[jp][ja][jc][jm] = f[jp-p[i]][ja-a[i]][jc-c[i]][jm-m[i]] + g[i];
                                vis[i][jp][ja][jc][jm] = 1;  //标记加入了哪些点
                            }
                        }
                    }
                }
            }
        }
        ll ans[maxn] = {0};
        for( ll i = n; i >= 1; i -- ) {  //记录路径
            if( vis[i][P][A][C][M] ) {
                ans[++ans[0]] = i-1;
                P -= p[i], A -= a[i], C -= c[i], M -= m[i];
            }
        }
        cout << ans[0] << endl;
        cout << ans[1];
        for( ll i = 2; i <= ans[0]; i ++ ) {
            cout << " " << ans[i];
        }
        cout << endl;
        return 0;
    }
    

      

    彼时当年少,莫负好时光。
  • 相关阅读:
    ASP.NET 缓存 @ OutputCache
    转:Windows下JDK1.6.0+Tomcat6.0的安装配置
    bit、byte、位、字节、汉字、字符之间的区别
    JDK1.6 tomcat 6.0环境变量配置
    DataList和Repeater里的自定义button控件的使用
    Button控件的onclick、oncommand、commandname、CommandArgument的区别
    GridView里的Button控件用法
    ASP.NET2.0 文本编辑器FCKeditor用法
    应用系统架构设计补全篇
    java.util.vector中的vector的详细用法
  • 原文地址:https://www.cnblogs.com/l609929321/p/9377635.html
Copyright © 2020-2023  润新知