1. 程式人生 > 實用技巧 >[CF1316E]Team Building

[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;
}