1044 状态压缩·一

m很小,可以用状态压缩做法,最多1<<m个状态,i每右移一位,状态随着要左移一位,判断q的限制,只需判断当前状态二进制中1的个数即可

 

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

int d[1010][2048];
//求整数二进制中1的个数
bool is_state_ok(int s,int q)
{
    int c = 0;
    for(; s; c++)
    {
        s &= (s-1);
    }
    return c <= q;
}

int w[2048];

int main(){

    //freopen("1.txt","r",stdin);

    int n,m,q;
    scanf("%d %d %d",&n,&m,&q);
    for(int i = 0; i < n; i++)
        scanf("%d",&w[i]);

    int MASK = (1<<m)-1;

    int maxn = 0;
    memset(d,0,sizeof(d));

    d[0][0] = 0; d[0][1] = w[0];
    for(int i = 1; i < n; i++)
        for(int j = 0; j <= MASK; j++)
            if(is_state_ok(j&MASK,q))
            {
                int state = (j<<1) & MASK;
                d[i][state] = max(d[i][state],d[i-1][j]);
                int state2 = (j<<1) & MASK | 1;
                if(is_state_ok(state2,q))
                {
                    d[i][state2] = max(d[i][state2],d[i-1][j]+w[i]);
                }
            }
    for(int j = 0; j <= MASK; j++)
        maxn = max(maxn,d[n-1][j]);
    printf("%d\n",maxn);
    return 0;
}

 

 

posted on 2015-11-20 14:56  zyz913614263  阅读(178)  评论(0编辑  收藏  举报

导航