zoukankan      html  css  js  c++  java
  • 【BZOJ1231】【Usaco2008 Nov】mixup2 混乱的奶牛(状压dp)

    1231: [Usaco2008 Nov]mixup2 混乱的奶牛

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 1586  Solved: 916
    [Submit][Status][Discuss]

    Description

    混乱的奶牛 [Don Piele, 2007] Farmer John的N(4 <= N <= 16)头奶牛中的每一头都有一个唯一的编号S_i (1 <= S_i <= 25,000). 奶牛为她们的编号感到骄傲, 所以每一头奶牛都把她的编号刻在一个金牌上, 并且把金牌挂在她们宽大的脖子上. 奶牛们对在挤奶的时候被排成一支"混乱"的队伍非常反感. 如果一个队伍里任意两头相邻的奶牛的编号相差超过K (1 <= K <= 3400), 它就被称为是混乱的. 比如说,当N = 6, K = 1时, 1, 3, 5, 2, 6, 4 就是一支"混乱"的队伍, 但是 1, 3, 6, 5, 2, 4 不是(因为5和6只相差1). 那么, 有多少种能够使奶牛排成"混乱"的队伍的方案呢?

    Input

    * 第 1 行: 用空格隔开的两个整数N和K

    * 第 2..N+1 行: 第i+1行包含了一个用来表示第i头奶牛的编号的整数: S_i

    Output

    第 1 行: 只有一个整数, 表示有多少种能够使奶牛排成"混乱"的队伍的方案. 答案保证是 一个在64位范围内的整数.

    Sample Input

    4 1
    3
    4
    2
    1

    Sample Output

    2

    输出解释:

    两种方法分别是:
    3 1 4 2
    2 4 1 3
     

     
    n<=16所以状压DP。
    f[i][j]记录当前牛的集合的状态为i,以牛j结尾的方案数。
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<ctime>
    #include<cctype>
    #include<algorithm>
    #include<cstring>
    #include<iomanip>
    #include<queue>
    #include<map>
    #include<set>
    #include<bitset>
    #include<vector>
    #include<stack>
    #include<cmath>
    #define ri register int
    #define ll long long
    #define For(i,l,r) for(ri i=l;i<=r;i++)
    #define Dfor(i,r,l) for(ri i=r;i>=l;i--)
    using namespace std;
    const int N=17;
    int n,m,a[N];
    ll ans,f[(1<<N)+N][N];
    inline ll read(){
        ll f=1,sum=0;
        char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
        while(isdigit(ch)){sum=(sum<<1)+(sum<<3)+(ch^48);ch=getchar();}
        return f*sum;
    }
    int main(){
        n=read(),m=read();
        For(i,0,n-1) a[i]=read(),f[1<<i][i]=1;
        For(i,0,(1<<n)-1){//枚举当前牛的集合 
            For(j,0,n-1){
                if(i&(1<<j)){//枚举当前集合中最后一个牛 
                    For(k,0,n-1){
                        if(abs(a[k]-a[j])>m&&!(i&(1<<k))) f[i|(1<<k)][k]+=f[i][j];
                    }
                }
            }
        }
        For(i,0,n) ans+=f[(1<<n)-1][i];
        printf("%lld",ans);
        return 0;
    }
  • 相关阅读:
    poj 3280 Cheapest Palindrome(区间DP)
    POJ 2392 Space Elevator(多重背包)
    HDU 1285 定比赛名次(拓扑排序)
    HDU 2680 Choose the best route(最短路)
    hdu 2899 Strange fuction (三分)
    HDU 4540 威威猫系列故事――打地鼠(DP)
    HDU 3485 Count 101(递推)
    POJ 1315 Don't Get Rooked(dfs)
    脱离eclipse,手动写一个servlet
    解析xml,几种方式
  • 原文地址:https://www.cnblogs.com/jian-song/p/11773808.html
Copyright © 2011-2022 走看看