百度之星-1001 调查问卷

链接

题意:

有m个问题。 n个人分别对m个问题有自己的解法,仅仅有AB两种。(相当于n条长度为m的01序列),在m中任意选取问题,使得序列在相应的位置上的答案不同的序列有至少k对,问选取问题的方案数。

解法:状态压缩DP,DP[i][state] 表示 state状态在前i行一共有几对不同。

那么状态转移方程为: DP[i][state]  = DP[i-1][state] + i-1 - vis[andlast]。 加号后面是什么意思呢? 每一行的状态最多增加 i-1贡献。  因此再减去重复的,剩下就是第i行的贡献了。andlast是state& cur[i] ,相当于state规范了选哪些行,和cur[i]按位与之后,剩下的就是第i行在state规范下的问题回答情况了。


代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<sstream>
#include<algorithm>
#include<iostream>
#include<stack>
#include<vector>
#include<set>
#include<map>

using namespace std;

const int maxn = 1050;
typedef long long ll;

int cur[maxn];
char st[maxn];
int dp[maxn][1<<11], vis[1<<11];

int main()
{
    int t;
    cin >> t;
    int cas = 1;
    while(t--)
    {
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1; i<=n; i++)
        {
            scanf("%s",st+1);
            cur[i] = 0;
            for(int j=1; j<=m; j++)
            {
                if(st[j] == 'A')
                    cur[i] += (1<<(m-j));
            }
        }
        printf("Case #%d: ",cas++);
        if(n*(n-1)/2 <k)
            printf("0\n");
        else
        {
            memset(dp,0,sizeof(dp));
            int high = 1<<m;
            int ans = 0;
            for(int state = 0; state<high; state++)
            {
                memset(vis,0,sizeof(vis));
                for(int j=1; j<=n; j++)
                {
                    int andLast = state & cur[j];
                    dp[j][state] = dp[j-1][state] + (j-1) - vis[andLast];
                    vis[andLast]++;
                }
            }
            for(int state = 0; state < high ; state++)
                if(dp[n][state] >= k)
                    ans++;

            printf("%d\n",ans);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37591656/article/details/81427818