zoukankan      html  css  js  c++  java
  • BZOJ 1231: [Usaco2008 Nov]mixup2 混乱的奶牛

    Time Limit: 10 Sec Memory Limit: 162 MB
    Submit: 1300 Solved: 763
    [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

    解题思路

    比较基础的状压dp,设dp[i][S] 表示当前状态为S,最后一头牛的编号为i的方案,转移时枚举一个j,判断j不在集合中并且(a[j]-a[i])>k ,转移方程 dp[j][S|(1<

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define LL long long
    
    using namespace std;
    
    int n,k,a[20];
    LL dp[20][1<<17],ans;
       //dp[i][S]表示现在状态为S,最后一位是第i个。 
    int main(){
        scanf("%d%d",&n,&k);
        for(register int i=1;i<=n;i++) scanf("%d",&a[i]);
        dp[0][0]=1;
    //  for(register int i=1;i<=n;i++) dp[i][(1<<i)]=1;
        a[0]=-0x3f3f3f3f;
        for(register int S=0;S<1<<n;S++)    
            for(register int i=0;i<=n;i++)if(((1<<i-1)&S) || !i){
                for(register int j=1;j<=n;j++)
                    if(!((1<<j-1)&S) && abs(a[j]-a[i])>k)
                        dp[j][(S|(1<<j-1))]+=dp[i][S];
            }
    //  for(register int i=1;i<=n;i++)
    //      for(register int j=0;j<1<<n;j++)
    //          cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
        for(register int i=1;i<=n;i++)
            ans+=dp[i][(1<<n)-1];
        printf("%lld",ans);
        return 0;
    }
  • 相关阅读:
    CodeForces 659F Polycarp and Hay
    CodeForces 713C Sonya and Problem Wihtout a Legend
    CodeForces 712D Memory and Scores
    CodeForces 689E Mike and Geometry Problem
    CodeForces 675D Tree Construction
    CodeForces 671A Recycling Bottles
    CodeForces 667C Reberland Linguistics
    CodeForces 672D Robin Hood
    CodeForces 675E Trains and Statistic
    CodeForces 676D Theseus and labyrinth
  • 原文地址:https://www.cnblogs.com/sdfzsyq/p/9676947.html
Copyright © 2011-2022 走看看