【题解】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 <= 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
思路
- 用二进制表示牛的状态state
- 设$f[state][last]$ 表示牛的排列状态为state,最后一头牛为last时的合法队列数
- 当且仅当状态j中没有包括第i只牛,且$abs(s[i]-s[last]) > k$ 时,第i只牛可以加入队列中
- 此时可以有转移方程$dp[State|(1<<i)][i]+=dp[State][last]$
注意 边界条件为 $dp[i][1<<i]=1$
- 最终答案
$$answer= \sum_{i=0}^{n-1}dp[(1<<n)-1][i]$$
代码
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
#define ll long long
using namespace std;
inline int read(){
int x=0,w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
return x*w;
}
const int maxs=(1<<16)+5,maxn=16+5;
int n,k;
ll ans;
ll dp[maxs][maxn],num[maxn];
int main() {
n=read(),k=read();
for(re i=0;i<n;++i) num[i]=read(),dp[1<<i][i]=1;
for(re S=0;S<(1<<n);++S) for(re i=0;i<n;++i) //枚举每一个状态
if(S&(1<<i)) for(re j=0;j<n;++j) //第j只牛是否在状态i中,进一步枚举没有在状态i中的牛
if(!(S&(1<<j))&&abs(num[j]-num[i])>k) //如果k不在队列中且差值大于k
dp[S|(1<<j)][j]+=dp[S][i];
for(re i=0;i<n;++i) ans+=dp[(1<<n)-1][i];
printf("%lld\n",ans);
return 0;
}
【题解】Luogu2915 [USACO08NOV]奶牛混合起来Mixed Up Cows的更多相关文章
- Luogu2915 [USACO08NOV]奶牛混合起来Mixed Up Cows (状压DP)
枚举末位状态 #include <iostream> #include <cstdio> #include <cstring> #include <algor ...
- 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 解题报告
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题意: 给定一个长\(N\)的序列,求满足任意两个相邻元素之间的绝对值之差不超过\(K\)的这个序列的排列有多少个? 范围: ...
- 洛谷P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...
- 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a u ...
- [USACO08NOV]奶牛混合起来Mixed Up Cows
题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i & ...
- 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 & ...
- [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 & ...
- P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
题目描述 约翰家有N头奶牛,第i头奶牛的编号是Si,每头奶牛的编号都是唯一的.这些奶牛最近 在闹脾气,为表达不满的情绪,她们在挤奶的时候一定要排成混乱的队伍.在一只混乱的队 伍中,相邻奶牛的编号之差均 ...
- 洛谷 P2915 【[USACO08NOV]奶牛混合起来Mixed Up Cows】
类似于n皇后的思想,只要把dfs表示放置情况的数字压缩成一个整数,就能实现记忆化搜索了. 一些有关集合的操作: {i}在集合S内:S&(1<<i)==1: 将{i}加入集合S:S= ...
随机推荐
- 基于RestAssured实现接口自动化
RestAssured是一款强大的接口自动化框架, 旨在使用方便的DSL,简化的接口自动化. 下面是基于RestAssured扩展的一个简单框架示例, 先看看用例的风格: package testca ...
- Windows进程间通讯(IPC)----管道
管道的分类 管道其实际就是一段共享内存,只不过Windows规定需要使用I/O的形式类访问这块共享内存,管道可以分为匿名管道和命名管道. 匿名管道就是没有名字的管道,其支持单向传输数据,如果需要双向传 ...
- 消息队列RabbitMQ(五):死信队列与延迟队列
死信队列 引言 死信队列,英文缩写:DLX .Dead Letter Exchange(死信交换机),其实应该叫做死信交换机才更恰当. 当消息成为Dead message后,可以被重新发送到另一个交换 ...
- java集合-哈希表HashTable
一.简介 HashTable也是一种key-value结构,key-value不允许null,并且这个类的几乎全部的方法都加上了synchronized锁,来保证并发安全,由于加了锁所以性能方面会比较 ...
- Flutter 2.2 更新详解
Flutter 2.2 版已正式发布!要获取新版本,您只需切换到 stable 渠道并更新目前安装的 Flutter,或前往 flutter.cn/docs/get-started 从头开始安装. 虽 ...
- [刷题] 19 Remove Nth Node From End of List
要求 给定一个链表,删除倒数第n个节点 示例 1->2->3->4->5->NULL , n=2 1->2->3->5 边界 n是从0还是从1计 n不合 ...
- centos国内镜像下载
国内镜像下载 http://mirrors.aliyun.com/centos/6/isos/x86_64/ 如果需要下载centos 7 版本进入对应7的/isos/x86_64/ 选择minima ...
- C++课程设计 通讯录管理系统 原码及解析
设计题目:通信录管理系统 用C++设计出模拟手机通信录管理系统,实现对手机中的通信录进行管理. (一)功能要求 查看功能:选择此功能时,列出下列三类选择. A 办公类B 个人类C 商务类,当选中某类时 ...
- C# 技术体系简介
C# 语言 .Net Framwork .NET Core winform界面编程 WPF开发经验,熟悉C/S架构产品开发及架构和设计 DevExpress界面框架(其实就是基于微软的 Winform ...
- Linux中级之keepalived配置
hacmp: ibm的高可用集群软件,并且是商业的(收费),一般用于非x86架构机器当中 AIX,Unix 去IOE:ibm,oracle,emckeepalived: 一款高可用集群软件,利用vrr ...