[HDU多校2020]第二场 1010 Lead of Wisdom

Lead of Wisdom

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6772

Problem Description
In an online game, “Lead of Wisdom” is a place where the lucky player can randomly get powerful items.

[HDU多校2020]第二场 1010 Lead of Wisdom_第1张图片
There are k types of items, a player can wear at most one item for each type. For the i-th item, it has four attributes ai,bi,ci and di. Assume the set of items that the player wearing is S, the damage rate of the player DMG can be calculated by the formula:

DMG=(100+∑i∈Sai)(100+∑i∈Sbi)(100+∑i∈Sci)(100+∑i∈Sdi)

Little Q has got n items from “Lead of Wisdom”, please write a program to help him select which items to wear such that the value of DMG is maximized.

Input
The first line of the input contains a single integer T (1≤T≤10), the number of test cases.

For each case, the first line of the input contains two integers n and k (1≤n,k≤50), denoting the number of items and the number of item types.

Each of the following n lines contains five integers ti,ai,bi,ci and di (1≤ti≤k, 0≤ai,bi,ci,di≤100), denoting an item of type ti whose attributes are ai,bi,ci and di.

Output
For each test case, output a single line containing an integer, the maximum value of DMG.

Sample Input

1
6 4
1 17 25 10 0
2 0 0 25 14
4 17 0 21 0
1 5 22 0 10
2 0 16 20 0
4 37 0 0 0
1
2
3
4
5
6
7
8

Sample Output

297882000

题面

[HDU多校2020]第二场 1010 Lead of Wisdom_第2张图片
[HDU多校2020]第二场 1010 Lead of Wisdom_第3张图片

思路

大佬给的题解
[HDU多校2020]第二场 1010 Lead of Wisdom_第4张图片

题意:
给了n个装备,每个装备属于一个类别,并且有四个属性a,b,c,d。每个类别只能选一个装备,最终伤害值为所有选定装备的四个属性分别与100求和再相乘。问DMG=(100+∑i∈Sai)(100+∑i∈Sbi)(100+∑i∈Sci)(100+∑i∈Sdi)这个值最大为多少。

读题考虑DFS暴搜?

令第 i 种装备的数量为 cnti,显然如果 cnti 不为 0 那么这一种装备不空的方案一定比空的方案优,在这时需要考虑的总方案数为 ∏max(cnti, 1),其中 ∑cnti ≤ 50。
最坏的情况肯定是所有的种类都相等,令它们都等于 k,则方案数为 k n k ^{n \over k} kn ,可以通过积分求导的方式求出极值,具体可见这个博客里写的:https://blog.csdn.net/wayne_lee_lwc/article/details/107556925
故当 k 取 3 时取到最大值 3 n 3 ^{n \over 3} 3n,而在 n = 50 时并不算太大,因此可以直接爆搜所有方案得到最优解。

但是这里我们想的是没有空节点的情况,即没有一种种类的装备的数量为0,如果有种类装备数量为零,而我们有不去预处理,直接也DFS去搜索的话,那么就会使搜索树上的这一层为空,相当于就是把前面的情况又复制了一遍,这样的话相当于每次枚举都会遍历这些层,所以最终的复杂度就变成了O( n ∗ 3 n 3 n*3^{n\over3} n33n)。 这样的话在原先基础上再乘n就会TLE了。
所以cnti = 0 的部分应该直接跳过,这里可以预处理,通过一个nxt数组来记录下一个cnti 不为0的种类的编号。

代码

#include
typedef long long ll;
const int N=55;
int Case,n,k,i,j,x,cnt[N],nxt[N],e[N][N][4];
ll ans;

//x表示当前的种类的编号,a,b,c,d为当前属性的值
void dfs(int x, int a, int b, int c, int d) {
    if (x > k) {  //递归出口
        ll tmp = 1LL * a * b * c * d;
        if (tmp > ans) ans = tmp;
        return;
    }
    int num = cnt[x];
    if (!num) {  //如果没有这个种类
        dfs(nxt[x], a, b, c, d);   //dfs下一个种类,此时a,b,c,d不变
        return;
    }
    //遍历该种类所有的装备,每一个都dfs
    for (int i = 1; i <= num; i++) {
        dfs(x + 1, a + e[x][i][0], b + e[x][i][1], c + e[x][i][2], d + e[x][i][3]);
    }
}

int main() {
    scanf("%d", &Case);
    while (Case--) {
        scanf("%d%d", &n, &k);
        for (i = 1; i <= k; i++) {
            cnt[i] = 0;
        }
        while (n--) {
            scanf("%d", &x);
            cnt[x]++; 
            for (j = 0; j < 4; j++) {
                scanf("%d", &e[x][cnt[x]][j]);
            }
        }
        //预处理nxt数组,存放每种装备下一个数量不为0的编号(1-51)
        x = k + 1;   //要到k+1,因为可能最后一种的装备没有
        for (i = k; i; i--) {
            nxt[i] = x;
            if (cnt[i]) {  //如果不为空
                x = i;
            }
        }
        ans = 0;
        dfs(1, 100, 100, 100, 100);
        printf("%lld\n", ans);
    }
}

你可能感兴趣的:(刷题)