题目描述

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

干了一下午的线段树合并和主席树还没整明白,想到自己还这么菜就很烦,看到一道状压DP能自己静下心来推了还是挺开心的。

如果$n$小于10就可以dfs回溯求了。

$f\left[ S\right] \left[ i\right]$表示集合$S$已被排列 且已第$i$个编号结尾的方案数

$f\left[ S|(1<<i)\right]\left[i \right ] =\sum  f\left[ s\right]\left[j \right ]$ 其中$j\in S\& \& \left| s_{i}-s_{j}\right| >k$

复杂度$O\left( n^{2}2^{n}\right)$

#include <bits/stdc++.h>
#define ll long long
using namespace std; inline int read() {
int x = , f = ; char ch = getchar();
while (ch < '' || ch > '') { if (ch == '-') f = -; ch = getchar(); }
while (ch >= '' && ch <= '') { x = x * + ch - ; ch = getchar(); }
return x * f;
} const int N = ;
ll f[<<N][N];
int t[N], n, k; int main() {
n = read(), k = read();
for (int i = ; i <= n; i++) t[i] = read();
f[][] = ;
int S = << n;
for (int s = ; s < S; s++) {
for (int i = ; i <= n; i++) {
int ii = i - ;
if (( << ii) & s) continue;
for (int j = ; j <= n; j++) {
int jj = j - ;
if (jj < ) { f[s | ( << ii)][i] += f[s][j]; continue; }
if (( << jj) & s && abs(t[j] - t[i]) > k) {
f[s | ( << ii)][i] += f[s][j];
}
}
}
}
ll ans = ;
for (int i = ; i <= n; i++) ans += f[S - ][i];
printf("%lld\n", ans);
return ;
}

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

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

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

  2. [USACO08NOV]Mixed Up Cows

    嘟嘟嘟 一看n那么小,那一定是状压dp了(表示从没写过,慌). 首先dp[i][j](i 是一个二进制数,第x位为1代表选了第x头牛),表示 i 这个状态最后一头牛是第 j 头牛时的方案数. 然后当 ...

  3. 【洛谷P2915】Mixed Up Cows

    题目大意:给定一个长度为 N 的序列,每个位置有一个权值,现要求重新排列这个序列,使得相邻的权值差的绝对值大于 K,求合法排列的方案数. 题解: 由于 N 很小,应该可以想到状压,考虑如何进行设计状态 ...

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

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

  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. 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

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

  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. P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

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

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

    类似于n皇后的思想,只要把dfs表示放置情况的数字压缩成一个整数,就能实现记忆化搜索了. 一些有关集合的操作: {i}在集合S内:S&(1<<i)==1: 将{i}加入集合S:S= ...

随机推荐

  1. redis常用命令及持久化机制

    redis  常用命令 查找redis服务文件 find / -name  redis-server 查找配置文件 find / -name redis.conf 启动服务时候,要指定配置文件 启动r ...

  2. Spark学习(1) Spark入门

    什么事spark Spark是一种快速.通用.可扩展的大数据计算引擎.项目是用Scala进行编写,基于内存计算的 包括交互式查询和流处理 spark内置项目 Spark SQL:是 Spark 用来操 ...

  3. c++项目经验分享

    1.C++的const比C语言#define更好的原因? 首先,它能够明确指定类型,有类型检查功能. 其次,可以使用C++的作用域规则将定义限制在特定的函数[常函数]或文件中. 第三,可以将const ...

  4. 关于Django数据库mysql连接错误问题Connection to api@localhost failed. [08001] Could not create connection to d

    Connection to api@localhost failed. [08001] Could not create connection to d 错误类型 django连接mysql数据库错误 ...

  5. 创建maven父项目以及子项目

    创建maven父项目以及子项目(Eclipse创建Maven Project跟Maven Module)https://blog.csdn.net/Mrsanger/article/details/8 ...

  6. jquery跨js文件调用函数示例

    var common_func; (function() { common_func = { load_hot_data: function(AreaCode) { var hot_html = &q ...

  7. pandas-13 时间序列操作方法pd.date_range()

    pandas-13 时间序列操作方法pd.date_range() 在pandas中拥有强大的时间序列操作方法. 使用 pd.date_range() 生成 'pandas.core.indexes. ...

  8. js删除对象里的某一个属性

    var a={"id":1,"name":"danlis"}; //添加属性 a.age=18; console.log(a); //结果: ...

  9. Linux目录结构说明

    文件系统层级标准(filesystem hierarchy standard,FHS). http://www.pathname.com/fhs/pub/fhs-2.3.html 以下是对这些目录的解 ...

  10. centos8 网络配置

    目录 centos8已经发布了,下载了一个体验一下,新安装好的centos8默认网卡是没有启动的,安装好后需要先配置网络.在/etc/sysconfig/network-scripts目录下存放着网卡 ...