题目描述

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。请数一数,有多少种队形是混乱的呢?

题目解析

本来想打状压搜索的,结果分析了一下发现会T。

看到一个特别好的思路,记在这里

相比于考虑用几个奶牛,再枚举两重状态的三循环来说,我们可以考虑先枚举状态,再枚举状态里用过的牛哪个在队伍最后。这样只要两重循环就可以了,效率比原来高多了。

dp[i][j]表示用了i个牛,状态是j(01串)

注意开longlong,这题很坑

Code

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std; int n,m;
long long ans;
int s[];
long long dp[][(<<)]; void init() {
for(int i = ; i <= n; i++) {
dp[i][<<(n-i)] = ;
}
return;
} bool judge(int j,int k) {
if(k == j) return false;
if(abs(s[j] - s[k]) > m) return true;
else return false;
} int main() {
scanf("%d%d",&n,&m);
for(int i = ; i <= n; i++) {
scanf("%d",&s[i]);
}
init();
int tmp;
for(int i = ; i < ( << n); i++) {
for(int j = ; j <= n; j++) {
if(dp[j][i]) continue;
if(i & ( << (n - j))) {
tmp = i ^ ( << (n - j));
for(int k = ; k <= n; k++) {
if(judge(k,j)) dp[j][i] += dp[k][tmp];
}
}
}
}
for(int i = ;i <= n;i++) {
ans += dp[i][(<<n)-];
}
printf("%lld",ans);
return ;
}

[USACO] 奶牛混合起来 Mixed Up Cows的更多相关文章

  1. 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 解题报告

    P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题意: 给定一个长\(N\)的序列,求满足任意两个相邻元素之间的绝对值之差不超过\(K\)的这个序列的排列有多少个? 范围: ...

  2. 洛谷P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

    P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...

  3. 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

    P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...

  4. [USACO08NOV]奶牛混合起来Mixed Up Cows

    题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...

  5. luogu P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

    题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...

  6. [USACO08NOV]奶牛混合起来Mixed Up Cows(状态压缩DP)

    题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...

  7. 【题解】Luogu2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

    题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...

  8. P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

    题目描述 约翰家有N头奶牛,第i头奶牛的编号是Si,每头奶牛的编号都是唯一的.这些奶牛最近 在闹脾气,为表达不满的情绪,她们在挤奶的时候一定要排成混乱的队伍.在一只混乱的队 伍中,相邻奶牛的编号之差均 ...

  9. 【USACO08NOV】奶牛混合起来Mixed Up Cows

    题目描述 约翰有 N 头奶牛,第 i 头奶牛的编号是 S i ,每头奶牛的编号都不同.这些奶牛最近在闹脾气, 为表达不满的情绪,她们在排队的时候一定要排成混乱的队伍.如果一只队伍里所有位置相邻的奶牛 ...

随机推荐

  1. 【OI】线性筛

    如何查找一个范围内的所有素数? 可以是从1~n挨个判断n%i 是否 == 0,也可以从 1~sqr(n) 一个个判断. 相信你们也听说过埃氏筛法,是使用每一个数的倍数筛掉合数!但是!每一个合数要被筛多 ...

  2. bootrap 手风琴Collapse源码分析

    /* ======================================================================== * Bootstrap: collapse.js ...

  3. shell脚本自动更新git

    gitpull.sh #!/bin/bash cd /home/wwwroot/default/mouse && git pull cd /home/wwwroot/default/s ...

  4. Oracle高水位线

    Oracle高水位线 https://blog.csdn.net/jx_jy/article/details/50607790 Oracle高水位线的概念 Oracle里面的对象放到存储级别都称为se ...

  5. openstack dnsmasq彭祖

    Openstack dnsmasq配置域名解析,openstackdnsmasq vi /etc/nova/nova.conf 在[DEFAULT]添加 dnsmasq_config_file=/et ...

  6. clc和clear命令的使用

    clc命令是用来清除命令窗口的内容,这点不用多说.不管开启多少个应用程序,命令窗口只有一个,所以clc无论是在脚本m文件或者函数m文件调用时,clc命令都会清除命令窗口的内容.clear命令可以用来清 ...

  7. 【转载】Cookie/Session机制详解

    [本文转自]http://blog.csdn.net/fangaoxin/article/details/6952954/ 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话. ...

  8. self , static 都是何方神圣?

    前言: php中 this  用于代指 对象, 而代指类的却有3个:self , static , parent self , static , parrent 既然都能代指类,那么他们之间又有哪些区 ...

  9. hadoop2.x 常用端口及定义方法

    一 常用端口号 1 HDFS 2 YARN 3 HBase 4 Hive 5 ZooKeeper 二 Web UIHTTP服务 1 对于存在 Web UIHTTP服务的所有 hadoop daemon ...

  10. SpringMVC实现Action的两种方式以及与Struts2的区别

    4.程序员写的Action可采用哪两种方式? 第一.实现Controller接口第二.继承自AbstractCommandController接口 5.springmvc与struts2的区别? 第一 ...