嘟嘟嘟

一看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. [javaSE] 数组(查找-二分查找)

    前提数组必须是有序的 定义最小,最大,中间的角标索引 int min,max,mid; min=0; max=arr.length-1; mid=(min+max)/2; 上面的索引需要变化,使用循环 ...

  2. WCF使用net.tcp传输文件

    摘要:今天看了一些官方的资料和配置,简单写了一个WCF服务来传递一个文件,借此看看WCF传输大文件的能力,这里采用的是NetTcp绑定,之所以没有采用 basicHttpBinding是因为考虑这种方 ...

  3. lua模块化

    main.lua local main = require("my") main.greeting() my.lua local foo = {} local function g ...

  4. POJ1426(KB1-E 暴力搜索)

    Find The Multiple   Description Given a positive integer n, write a program to find out a nonzero mu ...

  5. CSS响应式:根据分辨率加载不同CSS的几个方法,亲测可用

    有时候你需要把同一个页面在手机和pc同时打开,其中有一个办法就是判断不同分辨路加载不同的css 小编总结了几种分别加载css的方法: 1.比较复杂的使用js判断加载不同css (亲测可用) 但是这种方 ...

  6. DOM节点树和元素树--深度遍历

    我们在阅读JS高级程序设计的时候,提到了节点树的概念.比如说: elem.parentNode---找elem的父节点: elem.childNodes---找elem的所有的直接子节点: elem. ...

  7. Drupal8学习之路--官网文档碎碎记--主题篇

    主要记录一些琐碎的知识点. 1.“In Drupal 8 drupal_add_css(), drupal_add_js() and drupal_add_library()were removed ...

  8. C# 压缩图片到指定宽度,假如图片小于指定宽度 判断图片大小是否大于指定大小(KB) 如果大于则压缩图片质量 宽高不变

    class Program { static void Main(string[] args) {//G:\zhyue\backup\projects\Test\ConsoleApplication1 ...

  9. Pig parallel reduce并行执行数

    parallel语句可以附加到Pig Latin中任一个关系操作符后面,然后它会控制reduce阶段的并行,因此只有对与可以触发reduce过程的操作符才有意义.     可以触发reduce过程的操 ...

  10. java语言导学(5版)--第12章并发之二

    1不可变对象 概念:(immutable)对象创建后,状态不可更改.不可变对象在并发程序中尤其有用,因状态不可变,不会被线程干扰,也不会出现不一致状态. 书中通过实例是可变的类,并从此类衍生出一个不可 ...