zoukankan      html  css  js  c++  java
  • 【洛谷 2915】奶牛混合起来

    题目描述

    Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i <= 25,000). The cows are so proud of it that each one now wears her number in a gangsta manner engraved in large letters on a gold plate hung around her ample bovine neck.

    Gangsta cows are rebellious and line up to be milked in an order called 'Mixed Up'. A cow order is 'Mixed Up' if the sequence of serial numbers formed by their milking line is such that the serial numbers of every pair of consecutive cows in line differs by more than K (1 <= K <= 3400). For example, if N = 6 and K = 1 then 1, 3, 5, 2, 6, 4 is a 'Mixed Up' lineup but 1, 3, 6, 5, 2, 4 is not (since the consecutive numbers 5 and 6 differ by 1).

    How many different ways can N cows be Mixed Up?

    For your first 10 submissions, you will be provided with the results of running your program on a part of the actual test data.

    POINTS: 200

    约翰家有N头奶牛,第i头奶牛的编号是Si,每头奶牛的编号都是唯一的。这些奶牛最近 在闹脾气,为表达不满的情绪,她们在挤奶的时候一定要排成混乱的队伍。在一只混乱的队 伍中,相邻奶牛的编号之差均超过K。比如当K = 1时,1, 3, 5, 2, 6, 4就是一支混乱的队伍, 而1, 3, 6, 5, 2, 4不是,因为6和5只差1。请数一数,有多少种队形是混乱的呢?

    输入格式

    * Line 1: Two space-separated integers: N and K

    * Lines 2..N+1: Line i+1 contains a single integer that is the serial number of cow i: S_i

    输出格式

    * Line 1: A single integer that is the number of ways that N cows can be 'Mixed Up'. The answer is guaranteed to fit in a 64 bit integer.

    输入输出样例

    输入 #1
    4 1 
    3 
    4 
    2 
    1 
    
    输出 #1
    2 
    

    说明/提示

    The 2 possible Mixed Up arrangements are:

    3 1 4 2

    2 4 1 3

    题解:事实证明状压也不好做啊……得多做些状压题目了!

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    typedef long long ll;
    using namespace std;
    const int N=18;
    int n,k,f[N][1<<N],a[N];
    int main(){
        freopen("2915.in","r",stdin);
        freopen("2915.out","w",stdout);
        scanf("%d %d",&n,&k);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            f[i][1<<(n-i)]=1;
        int op=(1<<n);
        for(int i=1;i<op;i++)//枚举状态 
            for(int j=1;j<=n;j++){//枚举结尾奶牛 
                if(f[j][i]) continue;
                if(!(i&(1<<(n-j)))) continue;
                int ms=i^(1<<(n-j));
                for(int g=1;g<=n;g++){
                    if(g==j) continue;
                    if(a[j]-a[g]>k || a[g]-a[j]>k) f[j][i]+=f[g][ms];
                }
            
        }
        int ans=0;
        for(int i=1;i<=n;i++)
            ans+=f[i][(1<<n)-1];
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    gitlab 本地 定时备份
    centos 7 部署 汉化版 gitlab
    ELK开机启动 service文件内容
    通过 kms 激活 office 2016
    让 kibana 后台启动的方案
    centos7 yum 安装 redis
    域账户登录时提示“你的账户配置不允许使用这台电脑。请试一下其他电脑” 解决方案
    gitlab 接入 openldap、AD
    VS访问不到TFS、VS连接TFS报TF30063
    php--纯静态和伪静态的区别与关系
  • 原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11774452.html
Copyright © 2011-2022 走看看