CodeForces-1316E Team Building


传送门:CodeForces-1316E

题目描述

$n$个人,$p$个位置,选$p$个人作为相应位置的运动员,选$k$个人作为观众,每个人都有自己作为观众和相应位置的权值

求最大的权值和

输入描述

The first line contains $3$ integers $n,p,k$ $(2≤n≤10^5,1≤p≤7,1≤k,p+k≤n)$

The second line contains $n$ integers $a_1,a_2,…,a_n$ $(1≤a_i≤10^9)$

The $i$-th of the next $n$ lines contains $p$ integers $s_{i,1},s_{i,2},…,s_{i,p}.$ $(1≤s_{i,j}≤10^9)$

输出描述

Print a single integer res — the maximum possible strength of the club.

思路分析

由于$p$很小,所以可以状压dp,将运动员位置有无情况作为状态表示
$dp[i][j]$表示前$i$个人,状态为$j$的最大值

贪心的想,将每个人按观众实力排序,那么前$k+p$个人要么做观众要么做选手
这样对于观众来说是最优的,选手可以转移

样例输入

6 2 3
78 93 9 17 13 78
80 97
30 52
26 17
56 68
60 36
84 55

样例输出

377

AC代码

#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <vector>
#include <string>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define vi vector<int>
#define lowbit(x) x&(-x)
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);

using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10, M = 1 << 7;

struct node {
    int id, val;
} a[N];

int w[N][10];
ll dp[N][M];

int cmp(node a, node b) {
    return a.val > b.val;
}

int count(int x) {
    int cnt = 0;
    while (x) {
        if (x & 1)cnt++;
        x >>= 1;
    }
    return cnt;
}

int main() {
    IOS;
#ifdef xiaofan
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
#endif

    int n, p, k;
    cin >> n >> p >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].val;
        a[i].id = i;
    }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= p; j++)
            cin >> w[i][j];

    sort(a + 1, a + 1 + n, cmp);

    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 1 << p; j++) {
            dp[i][j] = dp[i - 1][j];
            int cnt = count(j);
            if (cnt < i && i - cnt <= k)
                dp[i][j] = dp[i - 1][j] + a[i].val;
            for (int s = 1; s <= p; s++)
                if (1 << (s - 1)&j)    dp[i][j] = max(dp[i][j], dp[i - 1][j ^ (1 << (s - 1))] + w[a[i].id][s]);
        }
    }

    cout << dp[n][(1 << p) - 1] << endl;



    return 0;
}


文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}