嘟嘟嘟

一看n那么小,那一定是状压dp了(表示从没写过,慌)。

首先dp[i][j](i 是一个二进制数,第x位为1代表选了第x头牛),表示 i 这个状态最后一头牛是第 j 头牛时的方案数。

然后当 j 被选上时,即 if(i & (1 << (j - 1)))时,我们在枚举倒数第二头牛p,也是当他存在时,且满足 abs(s[p] - s[j]) > k时,dp[i][j] += dp[i ^ (1 << (j - 1))][p],因为最后一头牛为p时,j 还没有被选,所以 j 这一位要亦或掉。

初始化就是只选一头牛的选法时一种,dp[1 << (i - 1)][i] = 1.

令 ms = (1 << n) - 1,则答案ans = sum(dp[ms][i]) (i = 1~n).

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<vector>
#include<cctype>
using namespace std;
#define space putchar(' ')
#define enter puts("")
#define Mem(a) memset(a, 0, sizeof(a))
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = ;
const int Maxs = 7e4;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = (ans << ) + (ans << ) + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar(x % + '');
} int n, k, a[maxn];
ll dp[Maxs][maxn]; int main()
{
n = read(); k = read();
for(int i = ; i <= n; ++i) a[i] = read(), dp[ << (i - )][i] = ;
int ms = ( << n) - ;
for(int i = ; i <= ms; ++i)
for(int j = ; j <= n; ++j)
if(i & ( << (j - )))
for(int p = ; p <= n; ++p)
if(p != j && (i & ( << (p - ))) && abs(a[p] - a[j]) > k)
dp[i][j] += dp[i ^ ( << (j - ))][p];
ll ans = ;
for(int i = ; i <= n; ++i) ans += dp[ms][i];
write(ans); enter;
return ;
}

[USACO08NOV]Mixed Up Cows的更多相关文章

  1. 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 & ...

  2. 解题报告 『[USACO08NOV]Mixed Up Cows(状压动规)』

    原题地址 观察数据范围:4 ≤ N ≤ 16. 很明显,这是一道状压DP. 定义:dp[i][j]表示队尾为奶牛i,当前含奶牛的状态为j,共有多少组符合条件的队伍. 代码实现如下: #include ...

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

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

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

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

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

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

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

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

  7. 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 & ...

  8. [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 & ...

  9. 【题解】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 & ...

随机推荐

  1. [转]c# winform tcp connect timeout 连接超时设置

    转自:https://www.cnblogs.com/jhlong/p/5622336.html 简单的c# TCP通讯(TcpListener) C# 的TCP Socket (同步方式) C# 的 ...

  2. css3动画运用

    https://daneden.github.io/animate.css/ https://minimamente.com/example/magic_animations/    http://i ...

  3. FFmpeg实现将图片转换为视频

    ##名称:ffmpeg实现将图片转换为视频 ##平台:ubuntu(已经安装好了ffmpeg工具) ##日期:2017年12月10日 简介: 因为学习需要,需要将连续图片转换成视频,昨天和今天早上用o ...

  4. LeetCode 544----Output Contest Matches

    During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, ...

  5. dbms_random 包的使用

    dbms_random是一个可以生成随机数值或者字符串的程序包. 这个包有 initialize(),seed(),terminate(),value(),normal(),random(),stri ...

  6. 跳过ssh在首次连接出现检查keys 的提示

    1.将需要登陆主机得公钥添加到known_hosts ssh-keyscan 192.168.77.129 192.168.77.130 >> /root/.ssh/known_hosts ...

  7. 回归JavaScript基础(四)

    主题:JavaScript变量.作用域和内存问题 JavaScript的变量和别的语言比起来是与众不同的.说道变量,不得不谈他的作用域.同很多语言一样,JavaScript开发者也不用担心开发中内存的 ...

  8. shell_script2

    一.函数 1.简介 Shell函数类似于Shell脚本,里面存放了一系列的指令 不过,Shell的函数存在于内存,而不是硬盘文件,所以速度很快 另外,Shell还能对函数进行预处理,所以函数的启动比脚 ...

  9. HDFS pipeline写 -- 客户端

    上一篇说了datanode端如何处理pipeline写请求的,这里主要看DFSClient. 这里以append为例, write差不多. 创建一个pipeline用于append操作的流程: Fil ...

  10. Azure 和 Linux

    Azure 正在不断集结各种集成的公有云服务,包括分析.虚拟机.数据库.移动.网络.存储和 Web,因此很适合用于托管解决方案. Azure 提供可缩放的计算平台,允许即用即付,而无需投资购买本地硬件 ...