zoukankan      html  css  js  c++  java
  • 2010-2011 ACM-ICPC, NEERC, Southern Subregional Contest C Explode 'Em All

    暴力枚举,状态压缩。

    枚举哪几行放,复杂度为$O(2^{25})$,大概有$3000$多万种情况。假设有$x$行放了,没放的那几行状态或起来为$st$,如果$st$中$1$的个数大于$x$,那么不可取;否则用$x$更新答案。

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<ctime>
    #include<iostream>
    using namespace std;
    typedef long long LL;
    const double pi=acos(-1.0),eps=1e-10;
    void File()
    {
        freopen("D:\in.txt","r",stdin);
        freopen("D:\out.txt","w",stdout);
    }
    template <class T>
    inline void read(T &x)
    {
        char c = getchar();
        x = 0;
        while(!isdigit(c)) c = getchar();
        while(isdigit(c))
        {
            x = x * 10 + c - '0';
            c = getchar();
        }
    }
    
    char s[30][30];
    int n,m,ans,num[30];
    int f[40000000];
    
    void dfs(int x,int cnt,int st)
    {
        if(cnt>=ans) return ;
        if(x==n)
        {
            if(f[st]<=cnt) ans=min(ans,cnt);
            return ;
        }
    
        dfs(x+1,cnt+1,st);
        dfs(x+1,cnt,st|num[x]);
    }
    
    int lowbit(int x)
    {
        return x&(-x);
    }
    
    int main()
    {
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    
        for(int i=1;i<(1<<25);i++) f[i]=f[i-lowbit(i)]+1;
    
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            for(int j=0;j<m;j++)
                if(s[i][j]=='*') num[i]=num[i]+(1<<j);
        }
    
        ans=min(n,m); dfs(0,0,0);
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    123
    p1216
    离线可持久化动态树
    线段树合并
    p2024
    树的dfs序,p1539,p1651,,2018/11/08模拟赛T3
    p1460
    CDQ分治,二维数点与三维数点,p1357与p2026与p2027与p2028与p2029
    自动AC机
    平衡二叉树之splay p1998
  • 原文地址:https://www.cnblogs.com/zufezzt/p/6361446.html
Copyright © 2011-2022 走看看