[CodeForces - 999F] Cards and Joy

链接:http://codeforces.com/problemset/problem/999/F


Problem Description
这里写图片描述

Examples

input
4 3
1 3 2 8 5 5 8 2 2 8 5 2
1 2 2 5
2 6 7
output
21

input
3 3
9 9 9 9 9 9 9 9 9
1 2 3
1 2 3
Output
0

Note
In the first example, one possible optimal card distribution is the following:

  • Player 1 gets cards with numbers [1, 3, 8]
  • Player 2 gets cards with numbers [2, 2, 8]
  • Player 3 gets cards with numbers [2, 2, 8]
  • Player 4 gets cards with numbers [5, 5, 5]

Thus, the answer is 2 + 6 + 6 + 7 = 21.

In the second example, no player can get a card with his favorite number. Thus, the answer is 0.


题目大意:有n个人以及 n * k 张牌,每个人分到 k 张牌。每张牌上都有一个数字,每个人都有一个喜爱的数字。若一个人的牌共有 i 个此人喜爱的数字,则此人的快乐值为h[i], h[0] = 0。求如何分配使得所有人的快乐值的和最大,输出该最大值。(其中 1 <= n <= 500, 1 <= k <= 10, 1 <= 牌上的数字, 人喜爱的数字 <= 1e5)


分析:易得快乐值的和 等于每个出现过的数字产生的快乐值的和。对于每个数字,i个喜欢该数字的人选 j 张该数字牌的快乐值和容易用dp处理出来,总时间复杂度为 O((n * k) ^ 2),最后输出它们的和即可。


代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5;
int dp[5001], c[N + 1], p[N + 1], h[501];
int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n * m; i++)
    {
        int tmp;
        cin >> tmp;
        c[tmp] += 1;
    }
    for (int i = 0; i < n; i++)
    {
        int tmp;
        cin >> tmp;
        p[tmp] += 1;
    }
    for (int i = 1; i <= m; i++)
        cin >> h[i];
    int ans = 0;
    for (int i = 0; i <= N; i++)
    {
        memset(dp, 0, c[i] + 1 << 2);
        for (int j = 1; j <= p[i]; j++)
            for (int k = c[i]; k; k--)
                for (int l = 1; k - l >= 0 && l <= m; l++)
                    dp[k] = max(dp[k], dp[k - l] + h[l]);
        ans += dp[c[i]];
    }
    cout << ans << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/carrot_iw/article/details/81320852