[CF1316E]Team Building

题目

传送门

题解

可以想到一个十分简陋的状态:定义 \(f[i][s]\) 为选到第 \(i\) 个人,队伍的空缺情况为 \(s\) 时的力量最大值,但是这有个问题——那些会变成观众的人该如何决策选还是不选?

对于观众,我们可以有一个十分显然的贪心——在那些还未被选中是队员的人中选前 \(k\) 个最大的(如果前 \(i\) 个剩下的不够,那就算剩下的都选,不用贪心),但是我们这个状态似乎不能记录对于状态 \(s\) 我们对应选了哪些人作为队员,而 \(i\) 这个人是否有可能被选作观众。

为了我们能够确定第 \(i\) 个人是否能被选中为观众,我们将所有的人按照 \(a\) 值进行排序,然后,对于 \(f[i][s]\) 这个状态的转移,我们考虑 \(i\) 在什么情况下能被选作观众,显然有

  1. \(i-1\) 个人能够足够 \(s\) 状态下队员的人数;
  2. 在前 \(i-1\) 个人选完队员后,第 \(i\) 个人必须是剩下的人中 \(a\) 值前 \(k\) 小的;

对于 \(1\) 我们可以用初始化极小值去掉这种不合法情况,对于 \(2\),由于我们已经将 \(a\) 值排序,那么剩下的人也一定是按照 \(a\) 值由大到小分布的,那么我们只需要判断剩下的人是否多于 \(k\) 个即可,即必须满足 \(i-\text{bitcnt}(s)\le k\),其中 \(\text{bitcnt}(x)\)\(x\) 在二进制下 \(1\) 的个数。

如果第 \(i\) 个人被选作队员,那么我们暴力枚举其被填在什么地方,从前一状态转移过来即可,这个转移就很简单了...

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
typedef long long LL;
typedef pair<int,int> pii;
#define ft first
#define sd second
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void read(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T read(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=1e5;
const int MAXP=7;
const int MAXSIZE=(1<<MAXP)-1;
const LL INF=(1ll<<60);

struct person{
    int a,s[MAXP+5];
    inline bool operator <(const person rhs)const{return a>rhs.a;}
}ps[MAXN+5];

int n,p,k,bitcnt[MAXSIZE+5];

LL f[MAXN+5][MAXSIZE+5];

inline void Init(){
    n=read(1),p=read(1),k=read(1);
    rep(i,1,n)ps[i].a=read(1);
    rep(i,1,n)rep(j,0,p-1)ps[i].s[j]=read(1);
    sort(ps+1,ps+n+1);
    rep(i,1,MAXSIZE)bitcnt[i]=bitcnt[i>>1]+(i&1);
}

inline void Get_f(){
    // memset(f,0xcf,sizeof f);
    int all=(1<<p)-1;
    rep(i,0,n)rep(s,0,all)f[i][s]=-INF;
    f[0][0]=0;
    rep(i,1,n)rep(s,0,all)if(bitcnt[s]<=i){//怎么可能队员比人还多...
        //这个人不当队员
        if(i-bitcnt[s]<=k)f[i][s]=Max(f[i][s],f[i-1][s]+ps[i].a);
        else f[i][s]=Max(f[i][s],f[i-1][s]);
        //这个人当队员
        rep(j,0,p-1)if((s>>j)&1)
            f[i][s]=Max(f[i][s],f[i-1][s^(1<<j)]+ps[i].s[j]);
    }
    // rep(i,1,n)rep(s,0,all)printf("f[%d, %d] == %d\n",i,s,f[i][s]);
    writc(f[n][all],'\n');
}

signed main(){
    Init();
    Get_f();
    return 0;
}
posted @ 2020-08-08 21:59  Arextre  阅读(133)  评论(0编辑  收藏  举报