E. Team Building(Codecraft-20+二进制dp)

E. Team Building(Codecraft-20+二进制dp)

E. Team Building

具体看代码:

把观众的贡献排序。选手二进制更新,异或哪里j的第k位为1,然后异或一下就是第k位为0,由此更新。

#pragma GCC optimize(3 , "Ofast" , "inline")

#include <bits/stdc++.h>

#define rep(i , a , b) for(register int i=(a);i<=(b);i++)
#define per(i , a , b) for(register int i=(a);i>=(b);i--)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int , int> pi;

template<class T>
inline void read (T &x) {
    x = 0;
    int sign = 1;
    char c = getchar ();
    while (c < '0' || c > '9') {
        if ( c == '-' ) sign = - 1;
        c = getchar ();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar ();
    }
    x = x * sign;
}

const int maxn = 1e5 + 10;
const int inf = int (1e9);
const ll INF = ll (1e18);
const double PI = 3.14159265358979;
const int mod = 1e9+7;
const double eps = 1e-8;
const int M = 1<<7+1;

ll dp[maxn][M];
ll s[maxn][10];
int n,p,k;
struct node{
    ll x;
    int id;
}e[maxn];

bool cmp(node A,node B) {
    return A.x>B.x;
}
int main () {
    read (n);read (p);read (k);
    rep (i,1,n) {
        read (e[i].x);
        e[i].id=i;
    }
    rep (i,1,n) {
        rep (j,1,p) {
            read (s[i][j]);
        }
    }
    sort (e+1,e+1+n,cmp);
    rep (i,0,n) {
        rep (j,0,(1<<p)+1) {
            dp[i][j]=-1;
        }
    }
    dp[0][0]=0;
    rep (i,1,n) {
        rep (j,0,(M-2)) {
            ll cnt = 0;
            rep (k,0,p-1) {
                if(j&(1<<k)) cnt++;
            }
            ll x = i-1-cnt;
            if(x<k) {
                if(dp[i-1][j]!=-1) dp[i][j]=dp[i-1][j]+e[i].x;
            }
            else {
                if(dp[i-1][j]!=-1) dp[i][j]=dp[i-1][j];
            }
            rep (k,0,p-1) {
                if(j&(1<<k)&&dp[i-1][j^(1<<k)]!=-1) {
                    dp[i][j]=max (dp[i][j],dp[i-1][j^(1<<k)]+s[e[i].id][k+1]);
                }
            }
        }
    }
    cout<<dp[n][(1<<p)-1]<<endl;
    return 0;
}


发布了259 篇原创文章 · 获赞 2 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/dy416524/article/details/105734144