E. Team Building

有$n$人,需要$p$个选手和$k$个观众。$(p\leq 7 ,k\leq 10^5)$
每个人当观众$+a_i$,当选手$+a_{i,p}$。求最大值。

假设$dp[i][j]$,选了$i$个观众,$j$为所选$p$的二进制状态。
转移显然$O(pn^22^p)$

考虑观众尽可能选大的,那么观众的$a_i$从大到到小排序。如果当不了选手,前面的人一定当观众
假设$dp[i][j]$,选到了$i$个人,$j$为所选$p$的二进制状态,所选观众=$min(i-num_p,k)$,$num_p$二进制中$1$的个数。

这样贪心的转移,只需要$O(n2^pp)$

代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
const int M = (1 << 7);
const int mod = 1e9 + 7;
ll dp[N][M];
struct node
{
int v, s[8];
} a[N];
bool cmp(node b, node d)
{
return b.v > d.v;
}
int n, p, k;
int main()
{
scanf("%d%d%d", &n, &p, &k);
int st = 1 << p;
for (int i = 1; i <= n; i++)
scanf("%d", &a[i].v);
for (int i = 1; i <= n; i++)
for (int j = 0; j < p; j++)
scanf("%d", &a[i].s[j]);
sort(1 + a, 1 + a + n, cmp);
for (int i = 0; i <= n; i++)
for (int j = 1; j < st; j++)
dp[i][j] = -1e18;
dp[0][0] = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < st; j++)
{
for (int k = 0; k < p; k++)
if (j & (1 << k))
dp[i][j] = max(dp[i - 1][j - (1 << k)] + a[i].s[k], dp[i][j]);
int p = __builtin_popcount(j);
if ((i - 1) - p >= k)
dp[i][j] = max(dp[i][j], dp[i - 1][j]);
else
dp[i][j] = max(dp[i][j], dp[i - 1][j] + a[i].v);
}
}
printf("%lld\n", dp[n][st - 1]);
}